常用SQL查询命令

448 阅读4分钟

0.数据表举例(students表)

id name score teacher
1 小张 86 Lisa
2 小王 90 Shirly
3 小李 76 Tom
4 小张 93 Sherry
5 小牛 86 Lisa

1.读取数据表的全部信息

select * from students

返回数据表中的所有行+所有列

2.读取数据表的部分信息

select name,teacher from students
仅返回两列数据
name teacher
小张 Lisa
小王 Shirly
小李 Tom
小张 Sherry
小牛 Lisa

3.读取去重后的信息

select distinct name from students
返回name列中,去重后的数据
name
小张
小王
小李
小牛

4.读取满足指定条件的信息

select * from students where score>90
返回满足“score>90”的全部数据
id name score teacher
4 小张 93 Sherry

5.where子句的运算符

运算符 描述
=、>、<、>=、<= 等于、大于、小于、大于等于、小于等于
between 在某个范围内
like 部分匹配
in 指定针对某个列的多个可能值
select * from students where teacher like 'Sh%'
返回teacher字段,以Sh开头的信息
id name score teacher
2 小王 90 Shirly
4 小张 93 Sherry
select * from students where score like '%6'
返回score字段,以6结尾的信息
id name score teacher
1 小张 86 Lisa
3 小李 76 Tom
5 小牛 86 Lisa
select * from students where teacher like '%i%'
返回teacher字段,包含字母i的信息
id name score teacher
1 小张 86 Lisa
2 小王 90 Shirly
5 小牛 86 Lisa
select * from students where teacher not like '%i%'
返回teacher字段,不包含字母i的信息
id name score teacher
3 小李 76 Tom
4 小张 93 Sherry

6.and & or运算符

select * from students where name='小张' and score>90
返回叫小张并且分数大于90的信息
id name score teacher
4 小张 93 Sherry
select * from students where score=90 or score=76
返回分数是90或者76的信息
id name score teacher
2 小王 90 Shirly
3 小李 76 Tom

7.对结果进行排序 order by

select * from students order by score
按照score升序排列,返回信息。PS:如果不特殊指定,order by默认按照升序排列
id name score teacher
3 小李 76 Tom
1 小张 86 Lisa
5 小牛 86 Lisa
2 小王 90 Shirly
4 小张 93 Sherry
select * from students order by score desc
按照score降序排列,返回信息
id name score teacher
4 小张 93 Sherry
2 小王 90 Shirly
1 小张 86 Lisa
5 小牛 86 Lisa
3 小李 76 Tom
order by多列的时候,先按照第一列排序,再按照第二列排序

8.分组group by

select name, sum(score) as total_score where score >80 group by name
在分数大于80的部分中,按name分组,返回分数总和
name score
小牛 86
小王 90
小张 179

9.limit子句

select * from students limit 3
返回3条记录
id name score teacher
1 小张 86 Lisa
2 小王 90 Shirly
3 小李 76 Tom

10.in操作符

select * from students where name in (小张,小牛)
返回名字为小张和小牛的信息
id name score teacher
1 小张 86 Lisa
4 小张 93 Sherry
5 小牛 86 Lisa

11.between操作符

select * from students where id between 2 and 4
返回id在2-4之间的信息,是否包括边界值,视不同数据库而定
id name score teacher
2 小王 90 Shirly
3 小李 76 Tom
4 小张 93 Sherry

12.SQL别名处理

select name as n, score as s, teacher as t from students
列别名:将name处理为别名n,score和teacher同理
id n s t
1 小张 86 Lisa
2 小王 90 Shirly
3 小李 76 Tom
4 小张 93 Sherry
5 小牛 86 Lisa
select a.xxx a.yyy b.zzz from 表1 as a, 表2 as b where ....
表别名:将表1处理为别名a,表2处理为别名b,并在select阶段使用别名

13.JOIN

join常用的操作:left join、right join、inner join
left join:返回左表中所有的记录,以及跟右表中的联接字段相等的记录
right join:返回右表中所有的记录,以及跟左表中的联接字段相等的记录
inner join:只返回两个表中联接字段相等的记录

详见 SQL JOIN命令

14.UNION、UNION ALL操作符

1. 从 "table1" 和 "table2" 表中选取所有不同的column_name值(只有不同的值)。注意:UNION 不能用于列出两个表中所有的属性值,UNION 只会选取不同的值。请使用 UNION ALL 来选取重复的值
select column_name from table1 union select column_name from table2

2. 从 "table1" 和 "table2" 表中选取所有的column_name(也有重复的值)
select column_name from table1 union all select column_name from table2 order by column_name