MySQL查询
select基础语法
select * from 表名字;select * from students;
select完整语法
select 去重选项 字段列表 [as 字段别名] from 数据源 [where子句] [group by 子句] [having子句] [order by 子句] [limit子句];- 查询所有字段- 查询指定字段- 使用 as 给字段起别名- 可以通过 as 给表起别名
条件查询where
使用where子句对表中的数据筛选,结果为true的行会出现在结果集中--语法select * from 表名 where 条件;例:select * from students where id=1;-----------------------------------------------------------比较运算符- 等于: =- 大于: >- 大于等于: >=- 小于: <- 小于等于: <=- 不等于: != 或 <>--逻辑运算符- and- or- not--模糊查询- like- %表示任意多个任意字符- \_表示一个任意字符--范围查询- in表示在一个非连续的范围内- between ... and ...表示在一个连续的范围内--空判断- 注意:null(占用空间地址的)与""(空字符串,不占空间地址的)是不用的- 判断空`is null`,这里注意不能用等于(=)- 判断非空`is not null`
比较运算符
#select .... from 表名 where .....#查询 大于18岁 的信息select * from students where age > 18;#查询小于18岁的信息select * from students where age < 18;#查询小于或者等于18岁的信息select * from students where age <= 18;#查询年龄为18岁的所有学生的名字select * from students where age = 18;#查询姓名不是影的select * from students where name != '影';
逻辑运算符
#18到28之间的所有学生信息select * from students where age >18 and age < 28;#18岁以上的女性select * from students where age > 18 and gender='女';# 查询编号小于4或没被删除的学生select * from students where id < 4 or is_delete = 0;#查询年龄不是18岁的 女性 这个范围内的信息select * from students where not (age = 18 and gender = '女'); -- 可读性 更高select * from students where age != 18 and gender = '女';#年龄不是小于或者等于18 并且是女性select * from students where (not age <= 18) and gender = 2;select * from students where age > 18 and gender = 2;#MySQL数据库中优先级:not>and>or
模糊查询
-- like-- % 替换任意多个-- _ 替换1个#查询姓名中 以 "小" 开始的名字select * from students where name like '小%'; -- '小于' '小张' '小xx'#查询姓名中 有 "小" 所有的名字select * from students where name like '%小%'; -- '小于' '小张' '小xx' 'x小x'#查询有2个字的名字select * from students where name like '__'; -- xx xx#查询至少有2个字的名字select * from students where name like '__%';#rlike 正则#查询以 周开始的姓名select name from students where name rlike "^周.*";#查询以 周开始、伦结尾的姓名select name from students where name rlike "^周.*伦$";
范围查询
#in (1, 3, 8)表示在一个非连续的范围内#查询 年龄为18、34的姓名select name from students where age in (18,34);#查询 姓名为影,老王的select * from students where name in ('影','python');#not in 不非连续的范围之内#年龄不是 18、34岁之间的信息select name from students where age not in (18,34);#between ... and ...表示在一个连续的范围内#查询 查询id是3到8的学生select * from students where id between 3 and 8;select * from students where id between 3 and 8;#查询学生id是3到8的男生select * from students where id between 3 and 8 and gender=1;select * from students where (id between 3 and 8) and gender=1; -- 可读性高#not between ... and ...表示不在一个连续的范围内#查询 年龄不在在18到34之间的的信息select * from students where age not between 18 and 34;select * from students where not age between 18 and 34;
空判断
#判空is null 不能用 =#查询身高为空的信息select * from students where name is null;#判非空is not nullselect * from students where name is not null;
聚合函数
- count(\*)表示计算总行数,括号中写星与列名,结果是相同的- max(列)表示求此列的最大值- min(列)表示求此列的最小值- sum(列)表示求此列的和- avg(列)表示求此列的平均值#总数#count查询学生总数select count(*) from students;#查询男性有多少人,女性有多少人select count(*) as 男性人数 from students where gender = 1;select count(*) as 女性人数 from students where gender = 2;#最大值max#查询最大的年龄select max(age) from students;#查询女性的最大编号select max(id) from students where gender = 2;#查询最大年龄的女性idselect max(age) from students where gender=2;#最小值min#查询未删除的学生最小编号select min(id) from students where is_delete=0;#求和sum#查询男生的年龄和#求和字段 是varchar则为0 如果字段内容是数字的话会进行相加select sum(age) from students where gender = 1;#平均值avg#查询未删除女生的年龄的平均值select avg(age) from students where gender=2 and is_delete = 0; -- 默认保留4位小数#计算平均年龄 sum(age)/count(*)#四舍五入 round(123.23 , 1) 保留1位小数#计算所有人的平均年龄,保留2位小数select round(avg(age),2) from students;select round(sum(age)/count(*),2) from students;
