2012-07-24 23 views
0

我有兩個名爲DataEntry_old和DataEntry_new的表,它們在兩個表上都有列doc id,mobileno,name,addr。如何顯示2個表中的所有重複行?

我想顯示兩個表中的所有匹配行,其中mobileno = 987654321。 也就是說,如果mobileno = 987654321在DataEntry_old中,則表中有一行,如果還有mobileno = 987654321在DataEntry_new中,則表中的另一行。

+5

** **什麼數據庫系統,以及哪個版本? * SQL *只是*結構化查詢語言* - 許多數據庫系統使用的語言,但不是數據庫產品......類似這樣的東西通常是供應商特定的 - 所以我們真的需要知道什麼**數據庫系統**你正在使用.... – 2012-07-24 09:38:25

+1

似乎是[union all]的工作(http://en.wikipedia.org/wiki/Set_operations_%28SQL%29#UNION_operator)。 – 2012-07-24 09:39:10

+1

歡迎來到StackOverflow!你可以編輯你的問題並添加適當的標籤(比如SQL-Server,MySQL等)。這將幫助我們幫助你! – Josien 2012-07-24 09:48:27

回答

0
SELECT doc_id, mobileno, name, addr 
FROM DataEntry_old 
    ,DataEntry_new 
WHERE DataEntry_ol.doc_id = DataEntry_new.doc_id 
+1

不要使用SQL-89風格的語法,特別是在教授新用戶時 - 在進行一些複雜的連接時,它有未定義的結果...'SELECT * FROM DataEntry_old O INNER JOIN DataEntry_new N ON O.mobileno = N.mobileno' – Basic 2012-07-24 09:57:06

+0

但它返回兩個表中匹配的數據在單個行中的兩次 – 2012-07-24 09:59:51

0
SELECT doc_id, mobileno, name, addr FROM DataEntry_old o inner join DataEntry_new n 
on o.doc_id=n.doc_id 
where n.mobileno='987654321' 
+0

試用N錯誤....使用了Union All ... thankx – 2012-07-24 10:14:59

2

您想使用UNION ALL此查詢:

select t.* 
from ((select 'old' as which, doc id, mobileno, name, addr 
     from DataEntry_old 
    ) union all 
     (select 'new' as which, doc id, mobileno, name, addr 
     from DataEntry_new 
    ) 
    ) t 
where t.mobileno='987654321' 
相關問題