一、查询所有
查询所有字段 MySQL:
select * from 表名
二、查询字段
查询指定字段 MySQL命令:
select stuid from students;
单字段查询:
select userId from 表名
多字段查询:
select userId,userName from 表名
字段别名:
select userId as ‘编号’,userName as ‘用户名’ from 表名
字段别名:
select userId ‘编号’,userName ‘用户名’ from 表名
三、去重
从查询结果中过滤重复数据:
select distinct,name from students;
四、聚合函数
1.sum()
select sum(字段名) from 数据表名;
2.avg()
select avg(字段名) from 数据表名;
3.max()
select max(字段名) from 数据表名;
4.min()
select min(字段名) from 数据表名;
5.count()
select count(字段名)from 数据表名;
五、条件查询
1.select * from 表名 where 条件;
2. 使用in关键字查询:
在某个范围:select * from 表名 where userID in(3,4,7)
3.在某个区间:select * from 表名 where userID between 1 and 5
4.模糊查询1:select * form 表名 where userName like ‘_三’
5.模糊查询2:select * form 表名 where userName regexp ‘.’ regexp 可以跟一些正则表达式
6.模糊查询3:select * form 表名 where userName like ‘%三’
六、分组排序
order by: 排序,升序asc,desc降序
group by:分组,适用于 '每’字出现的题目中,还要注意查询数据跟group by后面的字段是否相匹配
七、分页查询
select * from user_tb limit 3----只显示3条
select * from user_tb limit 3,5----从第3条后开始,然后一共显示5条
八、多表连接
表的连接
select * from a,b where a.id=b.id----两张表连接,使用主外键建立连接桥梁
九、子查询
select * from user_tb where userAge =(select max(userAge) from user_tb)----将查询的结果作为查询条件