数字属性列(包括 整数,浮点数)的条件筛选表格式
Operator(关键字) | Condition(意思) | SQL Example(例子) |
=, !=, < <=, >, >= | Standard numerical operators 基础的 大于,等于等比较 | col_name != 4 |
BETWEEN … AND … | Number is within range of two values (inclusive) 在两个数之间 | col_name BETWEEN 1.5 AND 10.5 |
NOT BETWEEN … AND … | Number is not within range of two values (inclusive) 不在两个数之间 | col_name NOT BETWEEN 1 AND 10 |
IN (…) | Number exists in a list 在一个列表 | col_name IN (2, 4, 6) |
NOT IN (…) | Number does not exist in a list 不在一个列表 | col_name NOT IN (1, 3, 5) |
字符串属性列的条件筛选表格式
Operator(操作符) | Condition(解释) | Example(例子) |
= | Case sensitive exact string comparison (notice the single equals)完全等于 | col_name = "abc" |
!= or <> | Case sensitive exact string inequality comparison 不等于 | col_name != "abcd" |
LIKE | Case insensitive exact string comparison 没有用通配符等价于 = | col_name LIKE "ABC" |
NOT LIKE | Case insensitive exact string inequality comparison 没有用通配符等价于 != | col_name NOT LIKE "ABCD" |
% | Used anywhere in a string to match a sequence of zero or more characters (only with LIKE or NOT LIKE) 通配符,代表匹配0个以上的字符 | col_name LIKE "%AT%" (matches "AT", "ATTIC", "CAT" or even "BATS") "%AT%" 代表AT 前后可以有任意字符 |
_ | Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) 和% 相似,代表1个字符 | col_name LIKE "AN_" (matches "AND", but not "AN") |
IN (…) | String exists in a list 在列表 | col_name IN ("A", "B", "C") |
NOT IN (…) | String does not exist in a list 不在列表 | col_name NOT IN ("D", "E", "F") |
DISTINCT
语法介绍
选取出唯一的结果的语法
SELECT DISTINCT column, another_column, … FROM mytable WHERE condition(s);
因为 DISTINCT
语法会直接删除重复的行, 我们还会学习 GROUP BY
语句, GROUP BY
也会返回唯一的行,不过可以对具有相同的 属性值的行做一些统计计算,比如:求和.
为了解决结果排序问题, 我们可以用 ORDER BY col_name
排序的语法来让结果按一个或多个属性列做排序.
结果排序(ordered results)
SELECT column, another_column, … FROM mytable WHERE condition(s) ORDER BY column ASC/DESC;
ORDER BY col_name
这句话的意思就是让结果按照 col_name 列的具体值做 ASC升序 或 DESC 降序,对数字来说就是升序 1,2,3,... 或降序 ... 3,2,1 . 对于文本列,升序和降序指的是按文本的字母序。
LIMIT
和 OFFSET
子句通常和ORDER BY
语句一起使用,当我们对整个结果集排序之后,我们可以 LIMIT
来指定只返回多少行结果 ,用 OFFSET
来指定从哪一行开始返回。你可以想象一下从一条长绳子剪下一小段的过程,我们通过 OFFSET
指定从哪里开始剪,用 LIMIT
指定剪下多少长度。
limited查询
SELECT column, another_column, … FROM mytable WHERE condition(s) ORDER BY column ASC/DESC LIMIT num_limit OFFSET num_offset;
你可以想象一下一个新闻网站的新闻条目数据,他们在页面上是按热度和时间排序的,每一个页面只显示10条数据,在所有这些属性都是不断变化的情况下。我们可以想见通过SQL的ORDER LIMIT OFFSET
句法,我们可以根据要求从数据库筛选出需要的新闻条目.