mysql里多表关系及查询语法
- 多表关系
-
一对多:在多的一方设置外键,关联一的一方主键
-
一对一 :用于表结构拆分,在其中任何一方设置外键(unique),关联另一方的主键
-
多对多:建立中间表,中间表包含两个外键,关联两张表的主键
2.多表查询
内连接
隐式:select ...from 表A,表B where 连接条件
显式:select... from 表A inner join 表B on连接条件
外连接
左外:select ...from 表A left join 表B on 连接条件
右外:select... from 表A right join 表B on 连接条件
自查询:
select...from 表A 别名 表B 别名 where 连接条件