使用WHERE
过滤SELECT
返回行。
SELECT select_list
FROM table_name
WHERE condition
ORDER BY sort_expression
如果在语句中使用了列别名,就不能使用WHERE
Operator | Description |
---|---|
= | 等于 |
> |
大于 |
< |
小于 |
>= |
大于等于 |
<= |
小于等于 |
<> or != |
不等于 |
AND | 逻辑与 |
OR | 逻辑或 |
IN | 包含 |
BETWEEN | 区间 |
LIKE | 相似匹配 |
IS NULL | 非空 |
NOT | 不是 |
从示例数据库中查询
查询first_name
等于Jamie
SELECT
last_name,
first_name
FROM
customer
WHERE
first_name = 'Jamie';
查询first_name
等于Jamie
和last_name
等于Rice
SELECT
last_name,
first_name
FROM
customer
WHERE
first_name = 'Jamie' AND
last_name = 'Rice';
查询last_name
等于Rodriguez
或first_name
等于Adam
SELECT
first_name,
last_name
FROM
customer
WHERE
last_name = 'Rodriguez' OR
first_name = 'Adam';
查询包含Ann、Anne、Annie
任何一个
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name IN ('Ann','Anne','Annie');
查询以Ann
开头的last_name
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name LIKE 'Ann%'
查询first_name
的长度大于等于 3 小于等于 5
SELECT
first_name,
LENGTH(first_name) name_length
FROM
customer
WHERE
first_name LIKE 'A%' AND
LENGTH(first_name) BETWEEN 3 AND 5
ORDER BY
name_length;
<>
查询first_name
以Bra
开头,而且last_name
不等于Motley
SELECT
first_name,
last_name
FROM
customer
WHERE
first_name LIKE 'Bra%' AND
last_name <> 'Motley';
<>
和!=
是相同的作用。