?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ? ?? ???? ?? ?? ??? ??? ?
2017-10-20
自连接:同一个数据表对其自身进行连接
mysql> SELECT s.type_id AS 子类ID, s.type_name AS 子类名称, p.type_name AS 父类名称 FROM goods_types AS s LEFT JOIN goods_types AS p ON s.parent_id = p.type_id;
mysql> SELECT s.type_id AS 子类ID, s.type_name AS 子类名称, p.type_name AS 父类名称 FROM goods_types AS s LEFT JOIN goods_types AS p ON s.parent_id = p.type_id;
2017-10-20
内连接:显示左表及右表符合连接条件的记录,A ∩ B
左外连接:显示左表中的全部和右表符合连接条件的记录,A ∪ (A ∩ B)
右外连接:显示右表中的全部和左表符合连接条件的记录,B ∪ (A ∩ B)
图片:http://oph264zoo.bkt.clouddn.com/17-10-20/73463873.jpg
左外连接:显示左表中的全部和右表符合连接条件的记录,A ∪ (A ∩ B)
右外连接:显示右表中的全部和左表符合连接条件的记录,B ∪ (A ∩ B)
图片:http://oph264zoo.bkt.clouddn.com/17-10-20/73463873.jpg
2017-10-20
分组条件HAVING:
对GROUP BY的查询结果进行过滤
在HAVING字句中,查询对象必须出现在查询表达式中或使用聚合函数
SELECT id, username, age, sex FROM users GROUP BY age HAVING sex = 1;
SELECT username, age, sex FROM users GROUP BY age HAVING count(id) > 0;
对GROUP BY的查询结果进行过滤
在HAVING字句中,查询对象必须出现在查询表达式中或使用聚合函数
SELECT id, username, age, sex FROM users GROUP BY age HAVING sex = 1;
SELECT username, age, sex FROM users GROUP BY age HAVING count(id) > 0;
2017-10-19
SELECT select_expr[, select_expr ...]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC], ...]
[LIMIT {[offset, ]row_count | row_count OFFSET offset}]
]
[
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC], ...]
[LIMIT {[offset, ]row_count | row_count OFFSET offset}]
]
2017-10-19
### 删除记录(单表删除)
如果省略WHERE,则该表所有记录都将被删除
DELETE FROM table_name [WHERE where_condition]
### 删除表:
DROP TABLE table_name;
删除没有被关联的普通表:直接上面的SQL语句就行了
删除被其他表关联的父表:
- 方法一:先删除子表,再删除父表
- 方法二:先删除父表的外键约束,再删除父表
如果省略WHERE,则该表所有记录都将被删除
DELETE FROM table_name [WHERE where_condition]
### 删除表:
DROP TABLE table_name;
删除没有被关联的普通表:直接上面的SQL语句就行了
删除被其他表关联的父表:
- 方法一:先删除子表,再删除父表
- 方法二:先删除父表的外键约束,再删除父表
2017-10-19