sql 主表的一条记录对应附表的多条记录语句
像这种情况我们用到会有
union 语句
sql union 语法
select column_name(s) from table_name1
union
select column_name(s) from table_name2
join left语句
left join 关键字语法
select column_name(s)
from table_name1
left join table_name2
on table_name1.column_name=table_name2.column_name
where 子查询
select * from a b where a.id=b.id
来现,下面看一例子。
select xxxx from 主表 inner join 附表 on 主表.xxxx=附表.xxxx where xxxx
实例说明。
employees_china:
e_id e_name
01 zhang, hua
02 wang, wei
03 carter, thomas
04 yang, ming
employees_usa:
e_id e_name
01 adams, john
02 bush, george
03 carter, thomas
04 gates, bill
列出所有在中国和美国的不同的雇员名:
select e_name from employees_china
union
select e_name from employees_usa
结果
e_name
zhang, hua
wang, wei
carter, thomas
yang, ming
adams, john
bush, george
gates, bill