2.1 - 2.2 Basics of Filtering with SQL
2.1用 where 子句(clause)
SELECT column1, column2, columnN
FROM table_name
WHERE [condition];
运算符
Operator | Description | |
---|---|---|
= | Equal | |
> | Greater than | |
< | Less than | |
>= | Greater than or equal | |
<= | Less than or equal | |
<> | Not equal. Note: In some versions of SQL this operator may be written as != | |
BETWEEN | Between a certain range | |
LIKE | Search for a pattern | |
IS NULL | Null value |
查找不匹配项,筛选出除了Alice mutton以外的东西
2.2 用IN OR NOT 三个operator
2.2.1 用IN()
要用()将需要筛选的值框起来生成list。list里面只能用str,都需要用到"
SELECT * FROM Customers
WHERE City IN ('Paris','London');
2.2.2 用 OR
如果满足第一个条件,就不会评估第二个条件(如果想要两个条件都满足需要用到 AND )
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
对比OR 优先用IN:1不用考虑顺序 2运行速度更快 3可以在子查询中用另一个选择语句(后面有)
2.2.3 用 AND
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
!!sql会先运行AND 然后运行 OR ,如果想将OR单独运行要用到()
2.2.4 用 NOT
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
2.3 通配符wildcards
Symbol | Description | Example |
---|---|---|
% | Represents zero or more characters | bl% finds bl, black, blue, and blob |
_ | Represents a single character | h_t finds hot, hat, and hit |
[] | Represents any single character within the brackets | h[oa]t finds hot and hat, but not hit |
^ | Represents any character not in the brackets | h[^oa]t finds hit, but not hot and hat |
- | Represents any single character within the specified range | c[a-b]t finds cat and cbt |
%wildcard不能匹配null
2.3.2 LIKE 运算符
SELECT * FROM Customers
WHERE City LIKE 'ber%';
2.4 ORDER BY 排序
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY 2,3
--直接按照2、3列顺序排序
ASC ascending升序
DESC descending降序
2.5 数字运算 + - * /
2.6 聚合函数 aggregate functions : AVG() COUNT() MIN() MAX() SUM()
计算平均值 AVG()
了解表中有多少记录 COUNT()
最大值最小值
2.7 分组GROUP BY
NULL 会被分组成null类别,这样就不会丢失部分数据
having count ,确保只选择≥2的订单。
select, from, where, group by, having