目录
四、在 值1,...,值n 里 和 在 值1 和 值2 之间
一、查看表中所有字段数据
select * from 表名 where 条件;
表名后可加where条件
select * from 表名 where 条件 order by 字段名 limit 条数;
二、查看表中部分字段数据
select 字段名1,字段名2,...,字段名n from 表名 where 条件;
表名后可加where条件
select 字段名1,字段名2,...,字段名n from 表名 where 条件 order by 字段名 limit 条数;
三、连接条件:and(并且)和or(或者)与关系运算符
1.连接条件: and (或者)
条件 == 替换为==> 条件1 and 条件2
select * from 表名 where 条件1 and 条件2;
2.连接条件: or (或者)
条件 == 替换为==> 条件1 or 条件2
select * from 表名 where 条件1 or 条件2;
条件
关系运算符
等于 字段名 = 值
不等于 字段名 != 值
小于 字段名 < 值
小于等于 字段名 <= 值
大于 字段名 > 值
大于等于 字段名 >= 值
四、在 值1,...,值n 里 和 在 值1 和 值2 之间
1.在 值1,...,值n 里
字段名 in (值1,...,值n);
select * from 表名 where 字段名 in (值1,...,值n);
2.在 值1 和 值2 之间
字段名 between 值1 and 值2;
select * from 表名 where 字段名 between 值1 and 值2;
五、为空和不为空
1.为空
字段名 is null
select * from 表名 where 字段名 is nul;
2.不为空
字段名 is not null
select * from 表名 where 字段名 is not nul;
六、模糊查询
1.%代替任意长度字符串
字段名 like “...%...”
select * from 表名 where 字段名 like “...%...”;
2._代替一个字符
select * from 表名 where 字段名 like “..._...”;
字段名 like “..._...”
八、正序排序和倒序排列
1.正序排列
order by 字段名
select * from teacher order by 字段名;
order by 字段名 asc
select * from teacher order by 字段名 asc;
2.倒序排列
order by 字段名 desc
select * from teacher order by 字段名 desc
七、限制条数(2种)
1.limit 条数
select * from teacher limit 条数
2.limit 偏移量,条数
select * from teacher limit 偏移量,条数
八、去重
select distinct 字段名 from 表名;
九、聚合函数
1.数量:count(字段名) count(* or 0)
select count(字段名) from 表名;
2.求和:sum(字段名)
select sum(字段名) from 表名;
3.平均值:avg(字段名)
select avg(字段名) from 表名;
4.最小值:min(字段名)
select min(字段名) from 表名;
5.最大值:max(字段名)
select max(字段名) from 表名;
select max(字段名),min(字段名)... from 表名;
十、分组
group by 字段名 having 条件;
十一、为字段名和表取别名
1.为字段取别名(as可以省略)
字段名(可写函数)as 字段别名
2.为表取别名(as可以省略)
表名 as 表别名
十二、交叉连接
select * from 表名1 cross join 表名2;
十三、内连接
select * from 表名1 inner join 表名2
on 表名1.表1字段名 = 表名2.表2字段名;
十四、左连接
select * from 表名1 left join 表名2
on 表名1.表1字段名 = 表名2.表2字段名;
十五、右连接
select * from 表名1 right join 表名2
on 表名1.表1字段名 = 表名2.表2字段名;
十六、复合条件
select --> from --> join --> on --> where --> group by --> having --> order by --> limit
十七、子查询 in
select * from 表名 where 字段 in (查询语句);
十八、子查询 exists
select * from 表名 where exists (查询语句);
十九、子查询 any
select * from 表名 where 字段 > any (查询语句);
二十、子查询 all
select * from 表名 where 字段 > all (查询语句);