复合查询SQL语句
select *
from customer left join orders
on customer.cust_id=orders.cust_id
????? ?如果是左连接该命令将返回join左侧表(customer)中的所有记录以及右侧表中匹配的记录;右连接则反之,返回orders中所有记录和customer的匹配记录;完全连接返回两个表中的所有记录。
customer:cust_id???? cust_name?????????
???????????????????? ?100?????? liujinzhong??????????????????
????????????????????? 101?????? liujinzhong1?????????????????
????????????????????? 102?????? liujinzhong2??????????????????
??????????????????????103?????? liujinzhong3
orders: cust_id????? cust_name??? orders_id
????????????????? ? 100??????? oraders_1??????? 1
??????????????????? 105??????? oraders_2??????? 2
?????????????????? ?106??????? oraders_3??????? 3????????????????
????????????????? ? 107??????? oraders_4??????? 4???????????????????????????????????????????????
????????????????????101??????? oraders_5??????? 5???????????????????????????????????????????????
??????????????????? 102??????? oraders_6??????? 6
以上面两张表为例
(1)select *
???? from customer a,orders b
???? where? a.cust_id=b.cust_id
结果集:
???????? cust_id???? cust_name?????????cust_id????? ? cust_name??? orders_id
????????? 100?????? liujinzhong?????????????????? 100???????? oraders_1??????? 1
????????? 101?????? liujinzhong1????????????????? 101??????? oraders_5??????? 5
????????? 102?????? liujinzhong2????????????????? 102??????? oraders_6??????? 6
(2)select *
???? from customer a left join orders b
???? on a.cust_id=b.cust_id
结果集:
cust_id???? cust_name?????????????????cust_id????? cust_name??? orders_id
? 100?????? liujinzhong?????????????????????? 100?????? ???? oraders_1??????? 1
? 101?????? liujinzhong1??????????????????? 101?????????? ?oraders_5??????? 5
??102?????? liujinzhong2????????????????????102??????????? oraders_6??????? 6
????????? 103?????? liujinzhong3?????????????---????????????????? ---------?????????? ?-
(3)select *
???? from customer a right join orders b
???? on a.cust_id=b.cust_id
结果集:
???????? cust_id???? cust_name?????????cust_id????? cust_name??? orders_id
????????? 100?????? liujinzhong?????????????????? 100??????? oraders_1??????? 1
????????? 101?????? liujinzhong1?????????????????101??????? oraders_5??????? 5
????????? 102?????? liujinzhong2????????????????? 102??????? oraders_6??????? 6
???????? ? ---?????? ------------??????????????????????? ? 105??????? oraders_2??????? 2
?????????? ---?????? ------------???????????????????????? ?106??????? oraders_3??????? 3
???????? ? ---?????? ------------????????????????????????? 107??????? oraders_4??????? 4
?(4)select *
???? from customer a full join orders b
???? on a.cust_id=b.cust_id
结果集:
???????? cust_id???? cust_name????????? cust_id????? cust_name??? orders_id
????????? 100?????? liujinzhong?????????????????? 100??????? oraders_1??????? 1
????????? 101?????? liujinzhong1????????????????? 101??????? oraders_5??????? 5
????????? 102?????? liujinzhong2????????????????? 102??????? oraders_6??????? 6
????????? 103?????? liujinzhong3????????????????? ---?????? ------------????? -?????
?????????? ?---?????? ------------??????????????????????? ?105??????? oraders_2??????? 2
?????????? ?---?????? ------------???????????????????????? ?106??????? oraders_3???????3
????????? ? ---?????? ------------????????????????????????? ?107??????? oraders_4????? 4????????????????????????????????????????????????
?????????????????????????????????????????????????