条件查询语法
SELECT column, another_column, …
FROM mytable
WHERE condition AND/OR another_condition AND/OR …`
语法规则
| 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 :关键字来指定某个或某些属性列唯一返回。
选取出唯一的结果的语法 (DISTINCT 语法会直接删除重复的行)
SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s) ;
ORDER BY :使结果按照 col_name 列的具体值做 ASC升序 或 DESC 降序
LIMIT和 OFFSET、limited查询
LIMIT和 OFFSET子句通常和ORDER BY 语句一起使用,用LIMIT来指定只返回多少行结果 ,用 OFFSET来指定从哪一行开始返回。
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;
用JOINs进行多表联合查询
INNER JOIN 只会保留两个表都存在的数据,
在表A 连接 B, LEFT JOIN保留A的所有行,不管有没有能匹配上B 反过来 RIGHT JOIN则保留所有B里的行。最后FULL JOIN 不管有没有匹配上,同时保留A和B里的所有行
常用统计函数:
| Function | Description |
|---|---|
| COUNT( * ) , COUNT( column ) | 计数!COUNT(*) 统计数据行数,COUNT(column) 统计column非NULL的行数. |
| MIN( column ) | 找column最小的一行. |
| MAX( column ) | 找column最大的一行. |
| AVG( column)**** | 对column所有行取平均值. |
| SUM( column ) | 对column所有行求和. |
HAVING 语法:对分组之后的数据再做SELECT筛选.
where与having的区别:
都是用来筛选的having是用来筛选组,where是用来筛选记录,即:where搜索条件在分组操作之前应用,having搜索条件在进行分组操作之后应用, 当一个查询包含了where条件和聚合函数,先执行条件过滤,再进行聚合函数,如:
SELECT SUM(score)
FROM sc
WHERE score > 60`
先过滤出score>60的记录,再进行SUM求和
having在聚合之后进行过滤,having在分组的时候会用,对分组结果进行过滤,通常分组里面包含聚合函数,如:
SELECT sid,AVG(score)
FROM sc GROUP BY sid
HAVING AVG(score) >60;
INSERT INTO 语句:向表格中插入新的行,也就是新增一条数据。
INSERT INTO 表名称 VALUES (值1, 值2,....)
注意:值一定要按字段顺序对应,字符串用引号包含,注意空格。
drop :删除整个表
drop 和 delete的区别:
drop 删表;delete 删除某一些数据
CREATE TABLE Customers :新建一个表名为Customers的表
Update 语句:用于修改表中的数据。
1,单字段Update
UPDATE 表名称 SET 列名称 = 新值 WHERE 条件
例:
UPDATE movies SET title='New title' WHERE id>2 and year <1998
把所有 id大于2 且 year小于1998 的所有数据的title 改成 'New title'
2,多字段Update
UPDATE 表名称 SET 列1 = 新值1, 列2 = 新值2 ... WHERE 条件
注意:UPDATE之前一定要先SELECT
DELETE 语句:用于删除表中的数据。
注意:DELETE之前一定要先SELECT