- 基本查询
| 查询所有列
select * from students;
| 指定条件查询
select * from students where is_delete=1;
| 查询指定列
select name, gender from students;
| 字段的顺序
select gender, name from students;
| as的使用
# 在查询指定列的同时,修改字段名显示
select 字段名 as 显示名 from 列表;
select name as 姓名, gender as 性别 from students;
# 在sql语句中暂时修改表名,减少代码量
select students.name, students.gender from students;
select s.name, s.gender from students as;
| 消除重复行
distinct 字段
select gender from students;
select distinct gender from students;
- 条件
| 比较运算符
>
select * from students where age > 18;
<=
select * from students where age <= 18;
| 逻辑运算符
and
select * from students where age>18 and gender=2;
or
select * from students where age>18 or height>=180;
not
select * from students where not (age>18 and gender=2);
| 模糊查询
--like
"%"
查找以某字符开头的名字
select name from students where name like "小%";
"_"
查询有几个字的名字
select name from students where name like "__";
select name from students where name like "___";
--rlike(支持正则)
select name from students where name rlike "^周";
select name from students where name rlike "^周.*伦$";
| 范围查询
-- in 表示在一个非连续范围内
select name from students where age in (18,34);
-- not in
select name , age from students where age not in (18,34);
【附:左右两个数字必须是表内含有的】
-- between ...and ... 表示在一个连续的范围内
select * from students where age between 18 and 34;
-- not between ...and.. 不需要加括号
select * from students where age not between 18 and 34;
【左右两个数字没有限制】
| 空判断
--判断is null 判断为空
select * from students where height is null;
--判断is not null 判断不为空
select * from students where height is not null;
- 排序
order by 字段
acs 升序 (没有asc也默认升序)
select * from students where (age between 18 and 34) and gender=1 order by age;
select * from students where (age between 18 and 34) and gender=1 order by age asc;
desc 降序
select * from students where (age between 18 and 34) and gender=2 order by height desc;
# 哪个排序操作在前,就先执行哪个
年龄从小到大,身高从高到矮的排序
select * from students order by age asc, height desc;
先对身高降序,如果体重相同,年龄升序
select * from students where (age between 18 and 34) and gender=2 order by height desc, age asc;
- 聚合函数
总数 (统计数量)
count
select * from students where gender=1;
select count(*) as 男生的人数 from students where gender=1;
select count(*) as 女生的人数 from students where gender=2;
# *的效率比选择的字段要高
select count(name) as 男生的人数 from students where gender=1;
最大值
max
select age from students;
select max(age) from students;
select max(height) from students where gender=2;
最小值
min
select sum(age) from students;
求和
sum
select sum(age) from students;
平均值
avg
select sum(age)/count(*) from students;
select avg(age) from students;
四舍五入 round(数字,保留位数)
select round(avg(age) ,2) from students where gender=1;
计算所有人的年龄,保留两位小数
select round(avg(age) ,2) from students;
- 分组
--group by
选择字段,
数据去重后分成一组
按照性别分组
select * from students group by gender; # 错误
select gender from students group by gender;
--group_concat
取出分组对应的
其他字段下的数据
select gender, group_concat(name) from students
group by gender;
select gender, group_concat(name, age) from students
group by gender;
--having
附加查询
每种性别的平均年龄
select gender, avg(age) from students
group by gender;
查询平均年龄超过30岁的性别
select gender, group_concat(name) from students
group by gender having avg(age)>30;
--with rollup
--在最后新增一行,记录当前列所有数的总和
select gender, count(*) from students group by gender with rollup;
-
分页
(limit start, count
0表示第一个)
1-操作
查询前5个数据
select * from students limit 0,5;
查询id4-8的数据
select * from students limit 3,5;
每页显示2个,第1,2,3个页面
select * from students limit 0,2;----每页数量*(页数-1)
select * from students limit 2,2;
select * from students limit 4,2;
每页显示2个,显示第3页的信息,按照年龄从小到大排序
select * from students limit 2*(4-1),2; 失败
select * from students limit 6,2 order by age asc; 失败
select * from students order by age asc limit 6,2 ;
2-加速查找
- 连表查询
-
inner join ...on..
内连接,取交集
--对inner join ...on...的理解
select * from goods inner join goods_brands
on goods.id=goods_brands.id;
分析:从goods某个范围内中查找数据,
而这个范围是含有goods_brands的交集,
且必须满足on后面的条件
--按照要求显示字段
select goods.name from goods inner join goods_brands
on goods.id=goods_brands.id;
--显示要求字段的同时,插入表中其他字段数据
select b.*,g.name from goods as g inner join goods_brands as b
on g.id=b.id;
--给数据表起短名字
select g.name from goods as g inner join goods_brands as b
on g.id=b.id;
-
left join ...on...
左连接以左边表为主
--左连接,以左表为基础,匹配另一个表数据,
--无数据用NULL表示
select b.*,g.name from goods as g left join goods_brands as b
on g.id=b.id;
--清除NULL的数据
select b.*,g.name from goods as g left join goods_brands as b
on g.id=b.id where b.name is not null;
-
自关联
(待补充)
create table areas(
aid int primary key,
atitle varchar(20),
pid int
);
查询所有省份
select * from areas where pid is null;
查询山东省的所有市
select * from areas as city inner join areas as province
on city.pid=province.aid where province.atitle="山东省";
数据库导入数据命令――source 文件.sql
- 子查询
- 标量子查询
返回一个数据(一行一列)
select * from students height > avg(height); 失败
select * from students where height > select avg(height) from students; 失败
select * from students where height > (select avg(height) from students);
- 列级子查询
返回的结果是一列(一列多行)
select name from classes where id in (select cls_id from students);
- 行级子查询
返回结果是一行(一行多列)
select * from students where (height,age) = (select max(height),max(age) from students);
- 组合Union
-- 组合,自动处理重合
select nickname from A union select name from B
-- 组合,不处理重合
select nickname from A union all select name from B