1 单表查询:
select * from
select sname from stu;
条件查询
select sname from stu where sid=2;
select sname from stu where sid>2;
select sname from stu where sid!=2;
查询时取别名,
select sid as 学号,sname as 姓名 from stu;
模糊查询,
select * from stu where sname like ‘小%‘; % 有多少相似查出多少,
select * from stu where sname like ‘小_‘; _ 有几条线,查出几条,,
select * from stu where sname like ‘小___‘; _ 有几条线,查出几条,,
排序 order by 字段名
select * from stu order by tzid 【asc】; 升
select * from stu order by tzid desc; 降
限制查询 limit
select * from stu limit 3; 可以理解为第 0 条开始往下 3条,
select * from stu limit 3,1 从第 3 条往下第 1 第,
平均值:
select avg(age) from dstu; 求培元年龄,
sql> select round(avg(age)) from dstu; 四舍五入平均值,统计
统计: 有几条数据
select count(age) from dstu;
求最大值
> select MIN(age) from dstu;
求最小值
select MIN(age) from dstu;
求和
select SUM(age) from dstu;
分级查询GROOP BY
统计出现的次数,
select count(*) from stu GROUP BY tzid; 可以看成是查询每个科目的报名人数,
统计每个科目报名人数,
select tzid,count(sid) from stu group by tzid;
科目 学生人数 学生表 科目
分组条件查询,having
select tzid as 科目,count(sid) as 学生人数 from stu group by tzid having 学生人数=2;
c潭州课堂25班:Ph201805201 MySQL第三课 (课堂笔记)
原文:https://www.cnblogs.com/gdwz922/p/9256686.html