2012-07-05 50 views
1

我只需要過濾這些在x和y列中具有相同值的錶行。從兩列中查找MySQL表的相同值

_______________________________________ 
| 
| x |  y  |  name  | 
________________________________________ 
| 
| 1  |  2  |  A   | 
|______________________________________ 
| 2  |  1  |  B  | 
|______________________________________ 
| 1  |  2 |  C   | 
|_______________________________________ 

最終的結果應該是我有A和C的結果。我需要過濾這些具有相同x和y值的行。

_______________________________________ 
| 
| x |  y  |  name  | 
________________________________________ 
| 
| 1  |  2  |  A   | 
|______________________________________ 
______________________________________ 
| 1  |  2 |  C   | 
|_______________________________________ 

我已經試過這段代碼,但我只設法使用一個字段。

select * 
from auto 
where x in (
     select x 
     from auto 
     group by x 
     having count(*) > 1 
    ); 
+0

編輯我的職務。 – 2012-07-05 17:07:25

+0

在上面給出的示例中,似乎將返回所有結果。行B也不匹配x和y。 – AllInOne 2012-07-05 17:07:28

+0

X和Y就像座標我需要找到具有相同座標的名稱 – 2012-07-05 17:19:25

回答

2
select * from 
auto a, (SELECT x, y, COUNT(*) FROM auto GROUP BY x, y HAVING COUNT(*) > 1) b 
where a.x = b.x 
and a.y = b.y 

將標誌着你的另一個問題是重複

3
SELECT t1.* 
FROM tablename t1, tablename t2 
WHERE t1.x = t2.x 
AND t1.y = t2.y 
AND t1.primary_key != t2.primary_key 
+0

查詢返回incorect值 – 2012-07-05 17:28:24