主要是一些数据的查询命令,对上一篇的补充:
消除重复行 --- distinct
select distinct 列名 from 表名;
例: select distinct genger from students;
查询的比较运算符
- 等于: =
- 大于: >
- 大于等于: >=
- 小于: <
- 小于等于: <=
- 不等于: != 或 <>
- 例1:查询编号大于3的学生
select* from students where id >3;
逻辑运算符
- and
- or
- not
- 例5:查询编号大于3的女同学
select * from students where id >3 and genger=0;
模糊查询
- like
- %表示任意多个任意字符
- _表示一个任意字符
- 例7:查询姓黄的学生
select * from students where name like '黄%';
范围查询
- in表示在一个非连续的范围内
- between ... and ...表示在一个连续的范围内
- 例10:查询编号是1或3或8的学生
select * from students where id in(1,3,8);
- 例: 查询编号为3至8的学生
select * from students where id between 3 and 8 ;
空判断
- 注意:null与 ' ' 是不同的
- 判空is null
- 判非空is not null
- 例13:查询没有填写身高的学生
select * from students where height is null;
优先级
- 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
- and比or先运算,如果同时出现并希望先算or,需要结合()使用
排序
- asc从小到大排列,即升序
- desc从大到小排序,即降序
select * from 表名
order by 列1 asc|desc,列2 asc|desc,...