SQL Server 2016 表操作:条件限制
2018-06-03 本文已影响26人
江湖十年
- 精确限制条件
... WHERE 字段=值;
- 模糊限制条件
... WHERE 字段 LIKE '值'; -- 值可以使用通配符 %、* 等
SELECT * FROM Student;
-- 查询以 na 开头
SELECT * FROM Student WHERE name LIKE 'na%';
-- 查询以 3 结尾
SELECT * FROM Student WHERE name LIKE '%3';
-- 查询包含 z
SELECT * FROM Student WHERE name LIKE '%z%';
image.png
- BETWEEN 指定范围
... WHERE 字段名 [NOT] BETWEEN 开始 AND 结束;
SELECT * FROM Student;
-- 查询 ID 为 9~11 的数据
SELECT * FROM Student WHERE ID BETWEEN 9 AND 11;
-- 查询 ID 不为 9~11 的数据
SELECT * FROM Student WHERE ID NOT BETWEEN 9 AND 11;
-- BETWEEN ... AND ... 不止可以用于数字,字符串、日期等字段类型也可以
SELECT * FROM Student WHERE name BETWEEN 'a' AND 'n';
image.png
image.png