SQL多個表實現聯合查詢

weixin_33711641發表於2013-11-22

select LineId,Id,Country from Domestic
union all
select LineId,Id,Country from Freedom
-- 聯合查詢Domestic,Freedom表的LineId,Id,Country all代表不去除反覆
--功能:[SQL語句] UNION [SQL語句]將兩個語句中選擇的同一列中的不同的值篩選出來

SELECT<表1>.<列名> ,<表2><列名>FROM<表1>OUTER JOIN<表2> ON<表1>.<列>=表2>.<列名>
--功能:實現兩個表的外連線

Select Domestic.LineId,Freedom.LineId from Domestic,Freedom where Domestic.Sames=Freedom.Sames
Select Domestic.LineId,Freedom.LineId FROM Domestic inner join Freedom on Freedom.Sames=Domestic.Sames
--功能:實現兩個表的內連線 把Domestic,Freedom兩個表用Domestic.Sames=Freedom.Sames關聯起來顯示Domestic.LineId,Freedom.LineId
------------------------
我的資料庫表是這種:table0101,table0102,table0103,.......各個表有同樣的結構,我想用sql語句從查詢分析器裡匯出來,有沒有辦法能夠一次匯出,語句要返回一個結果集.
用union all就能夠實現:

select * from table0101
union all
select * from table0102
union all
select * from table0103
union all
select * from table0104
....

****************
補充:假設想去掉反覆記錄的話,把union all 改成 union就能夠了
---
以上,希望對你有所幫助。

相關文章