记录语法及单表查询

tech2022-07-09  172

单表查询

#插入数据 insert into 表名(字段名 可以指定你要往哪个字段插入数据) values (插入的值,与前面一一对应) #插入查询的数据 insert into 表名(字段名) select(字段名) from 表名 where ;
#删除表内所有数据 delete from 数据库.表名 #指定表内数据删除 delete from 表名 where 条件 #清空整张表 truncate 表名;
#更新数据 update 表名 set 字段名 = 条件, where 条件;

单表查询

# 单表查询的详细语句 select distinct 字段名 from 表名: where 条件 group by 条件 #(分组) having 条件 order by 条件 #(排序) limit 条件 #(限制显示的条数) #关键字的执行顺序 where group by having distinct(去重) obder by limit

对查询出来的数据进行一定的操作

运算操作

select可以在字段名后添加(+ - * / )的操作,这样子显示出来的数据就是进行过运算的

数据拼接
#concat(字段名或要拼接的字符) select concat(id,':',name) from test; #若是要拼接的字符一致可以使用concat_ws() select concat_ws(':',id,name) from test; #as 可以为查出来的数据字段进行命名 select concat(id,':',name) as new_data from test;
where 可以使用哪些约束

​ 1.比较运算符:> < >= <= !=

​ 2.between 条件1 and 条件2 在两者之间的数据都可以包括两边

​ 3.in(条件) 查出的数据只要在括号内都可以

​ 4.like “字符 %或者_ ”模糊查询

​ 1.%:代表任意个字符

​ 2._:代表一个任意字符

​ 5.and or not

group by

group by 主要是进行了一个分组操作,使用该关键字多数是要对一个组进行操作,这种时候考虑单个数据是没有意义的

聚合函数(分组后使用)

count() select count()分组后计算一个组共有几个你查询的数据,会将每个组都显示出来 select count(id) from emp group by post; max() 查询一个组内最大的数 select max(salary) from emp group by post; min() 查询一个组内最小的数 select max(salary) from emp group by post; avg() 查询平均数 select max(salary) from emp group by post; sum() 查询和

group by有一个group_concat()可以对查询出来的数据进行拼接。

having

where 是在分组前进行一次过滤,having则是在分组后对组进行一次过滤

order by

order by 是进行排序的操作

语法

select * from emp order by salary; select * from emp order by salary asc;(升序 默认) select * from emp order by salary desc;(降序)
limit

对查出的数据进行限制

#查5条记录 select * from emp order by salary limit 5, #自定义从第几条开始查询 select * from emp order by salary asc limit 5,5; #从第五条开始查询,在查询5条
regexp

​ 在后面跟正则表达式既可以根据正则表达式将所需要的数据查出来。

#查出名字以ale开头的数据 SELECT * FROM employee WHERE name REGEXP '^ale';
最新回复(0)