-- like模糊查询: 效率低。 %替换任意个字符, _替换一个字符 selectnamefrom students wherenamelike"小%";
-- rlike 查询中使用正则 selectnamefrom students wherenamerlike"^小.*";
-- 范围查询 -- in (), not in(), between ... and ..., not between ... and ... selectnamefrom students where age in (1, 2, 3); select * from students where age between18and34;
-- 空判断: is null, is not null select * from students wherehighisnull;
-- 排序 -- order by 字段1 [asc(默认升序) / desc(降)], 字段2 [] ... select * from students orderby age asc, height desc;
-- 聚合函数 -- count(*)总数, max()最大值, min(), sum(), avg(), round(数, 精度)四舍五入 -- 男性的平均身高 保留2位小数 selectround(avg(height), 2) from students where gender=2;
-- 在students表中,按照性别分组,查询所有的性别 -- 失败select * from students group by gender; select gender from students groupby gender;
-- 在students表中,计算每种性别中的人数 select gender,count(*) from students groupby gender;
-- 在students表中,计算男性的人数 selectcount(*) from students where gender=1; select gender,count(*) from students where gender=1groupby gender;
-- group_concat(...) -- 在students表中,查询男性中的姓名 select gender, group_concat(name) from students where gender=1groupby gender; | gender | group_concat(name) | +--------+--------------------+ | 男 | tt1,tt2 | +--------+--------------------+
-- having -- 在students表中,按照性别分组,查询平均年龄超过30岁的性别,以及姓名 having avg(age) > 30 select gender, group_concat(name), avg(age) from students groupby gender havingavg(age)>30;
-- 在students表中,查询每种性别中的人数多于2个的信息 select gender, group_concat(name), count(*) from students groupby gender havingcount(*)>2;
-- 在students表中,查询前5个数据 offset=0省略 select * from students limit5;
-- limit (第N页-1)*每页的个数, 每页的个数 -- 在students表中,每页显示2个,第1个页面 select * from students limit0, 2; -- 在students表中,每页显示2个,第2个页面 select * from students limit2, 2; -- 在students表中,每页显示2个,第3个页面 select * from students limit4, 2;
-- 在students表中,查询女性信息 并且按照身高从高到矮排序 只显示前2个 select * from students where gender=2orderby height desclimit0,2;
连接查询
left join (左连接):返回左表中的所有记录和右表中连接字段相等的记录,不相等的记录的所有字段用null填充。
right join (右连接):返回右表。。。
inner join (等值连接/内连接):只返回两个表中连接字段相等的行。
full join (全外连接):返回左右表中所有的记录和左右表中连接字段相等的记录。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
-- 查询 有能够对应班级的学生以及班级信息 select * from students innerjoin classes on students.cls_id=classes.id;
-- 查询 有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称 select s.*, c.name from students as s innerjoin classes as c on s.cls_id=c.id;
-- 查询 有能够对应班级的学生以及班级信息, 并排序(先按班级进行排序,班级内按学生id排序)。 select c.name , s.* from students as s innerjoin classes as c on s.cls_id=c.id orderby c.name, s.id;
select * from students as s leftjoin classes as c on s.cls_id=c.id;
-- 查询没有对应班级信息的学生 -- select ... from xxx as s left join xxx as c on..... where/having ..... select * from students as s leftjoin classes as c on s.cls_id=c.id where c.id isnull;
子查询
1 2 3 4 5 6 7 8
-- 查询出高于平均身高的信息 select * from students where height > (selectavg(height) from students);
-- 查询最高的男生信息 select * from students where height = (selectmax(height) from students);
-- 查询学生的班级号能够对应的学生信息 select * from students where cls_id in (selectidfrom classes);
-- 创建areas表 createtable areas( aid int primary key, atitle varchar(20), pid int ); -- 导入数据 source areas.sql
-- 查询所有省份 select * from areas where pid isnull;
-- 查询出山东省有哪些市 select * from areas as province innerjoin areas as city on city.pid=province.aid having province.atitle="山东省"; select province.atitle, city.atitle from areas as province innerjoin areas as city on city.pid=province.aid having province.atitle="山东省";
-- 查询出青岛市有哪些县城 select province.atitle, city.atitle from areas as province innerjoin areas as city on city.pid=province.aid having province.atitle="青岛市"; select * from areas where pid=(select aid from areas where atitle="青岛市")
exists / not exists
exists / not exists子句根据其内查询语句的结果集空或者非空,返回一个布尔值。结果集不为空exists返回true, 为空exists放回false, not exists则相反。
-- 薪资分类 selectname, sal, (casewhen sal <= 1000then'C' when sal > 1000and sal <= 2000then'B' else'A'end) sal_class from sal; /* t1 1000.21 B t2 2000.21 A t3 3000.21 A t4 5000.21 A t5 100.21 C */
-- 按薪资分类统计平均薪资 select (casewhen sal <= 1000then'C' when sal > 1000and sal <= 2000then'B' else'A'end) sal_class , avg(sal) from sal groupby sal_class; /* B 1000.21 A 3333.543333 C 100.21 */
空值处理
ifnull(col, value)函数
1
select * from emp orderbyifnull(comm, -1);
事务
1 2 3 4 5
-- 关闭自动提交 set autocommit = 0; ....... -- 手动提交 commit;