2012-10-26 49 views
0

重複值的行我有表1:查詢顯示具有2列

id|name|city 
------------- 
1 | bat|nyc 
2 | cat|del 
2 | rat|bom 
3 | mat|xyz 

表2

id|name|city 
-------------- 
1 |bat|dsada 
2 |cat|sadasd 
3 |rat|sdasdas 
2 |rat|asdas 

我需要一個查詢,顯示其中兩個編號和名稱在兩個表中相同的行例如上面的查詢必須返回 我需要一個查詢,顯示的行

1|bat|nyc 
2|cat|del 

從第一臺

回答

0

嘗試加入表都像如下

select tabl1.* from table1 
inner join table2 
on table1.id=table2.id and table1.name=table2.name 
0

最簡單的(也可能是最effcient)是使用EXISTS

SELECT id,name,city FROM Table1 t1 
WHERE EXISTS 
(
    SELECT 1 FROM Table2 t2 
    WHERE t2.name = t1.name 
    AND t2.id = t1.id 
) 
0
select 
    T1.id, T1.name 
from Table1 as T1 
where 
    exists (select * from Table2 as T2 where T2.id = T1.id and T2.name = T1.name) 
0

嘗試這樣的: JOIN將確保兩個條件都滿足,即兩個表中的兩個記錄具有相同的值idname

Select t1.name, t1.city from Table1 t1 
Join Table2 t2 
    ON t1.id = t2.id and 
     t1.name = t2.name 
+0

「SELECT *」和「上」,而不是 – danihp

+0

感謝那個朋友 – codingbiz

+0

@codingbiz「其中」 - 你需要寫t1.name,t1.city否則它會給你一個不明確的列錯誤 –