一.常用的运算符
=:相等
!=:不等
>:大于
<:小于
>=:大于等于
<=:小于等于
IS NULL:为空
IS NOT NULL:不为空
in:在其中
like:模糊查询
BETWEEN...AND...:在两个条件之间
and:逻辑与
or:逻辑或
not:逻辑非
CASE
WHEN 判断条件 then 写入条件
END
二.查询
1.指定条件查询
格式:SELECT 查询列(可查询复数个) from 表名 WHERE 判断条件
例如我要查询Student表中Score大于90的数据
SELECT Id,Score from WHERE Score > 90
2.指定条件查询(多条件)
(1).使用and添加条件
格式:
SELECT 查询列 from 表名 判断条件1 and 判断条件2(可以使用复数个判断条件)
例如我要查询Student表中Score大于90并且Address为成都的数据
SELECT Score,Address WHERE Score > 90 and Address = '成都'
(2).使用between...and...判断在一个范围中的数据
格式:
SELECT 查询列 from 表名 WHERE 判断条件 BETWEEN ... AND ...
例如我要查询Student表中Score在80到90之间的数据
SELECT Score from Student WHERE Score BETWEEN 80 AND 90
(3).使用in查询
格式:
SELECT 查询列 from 表名 WHERE 判断条件
例如我要查询Student表中Address为成都和重庆的数据
SELECT Address from Student WHERE Address in('成都','重庆')
(4).使用when then语句
查询学生的生肖
SELECT *
casewhen year(Birth) % 12 = 4 then '鼠'when year(Birth) % 12 = 5 then '牛'when year(Birth) % 12 = 6 then '虎'when year(Birth) % 12 = 7 then '兔'when year(Birth) % 12 = 8 then '龙'when year(Birth) % 12 = 9 then '蛇'when year(Birth) % 12 = 10 then '马'when year(Birth) % 12 = 11 then '羊'when year(Birth) % 12 = 0 then '猴'when year(Birth) % 12 = 1 then '鸡'when year(Birth) % 12 = 2 then '狗'when year(Birth) % 12 = 3 then '猪'else ''--如果不属于其中的任何一个表示数据有错误,直接添加一个空字符串报错
end 生肖--自定义中文列名为生肖
from People
简写方式:
SELECT *
case year(Birth) % 12when 4 then '鼠'when 5 then '牛'when 6 then '虎'when 7 then '兔'when 8 then '龙'when 9 then '蛇'when 10 then '马'when 11 then '羊'when 0 then '猴'when 1 then '鸡'when 2 then '狗'when 3 then '猪'else ''--如果不属于其中的任何一个表示数据有错误,直接添加一个空字符串报错
end 生肖--自定义中文列名为生肖
from People
三.排序
desc:降序排列方式
asc:升序排列方式
1.根据条件排序
格式:
SELECT 列名 from 表名 order by 条件列 排序方式
例如我要查询Student表中Score按照降序排列方式排列的数据
SELECT * from Student order by Score desc
四.常用于排序的函数
len()用于计算字符串长度
year()用于获取数据中的年份
getdate()用于获取当前时间
month():用于获取数据中的月份
day():用于获取数据中的日期