1
此表:鮮明的多列
mysql> select * from work;
+------+---------+-------+
| code | surname | name |
+------+---------+-------+
| 1 | John | Smith |
| 2 | John | Smith |
+------+---------+-------+
我想獲得對代碼其中姓名是平等的,所以我這樣做:
select distinct A.code, B.code from work A, work B where A.name = B.name group by A.code, B.code;
不過,我得到以下結果返回:
+------+------+
| code | code |
+------+------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
+------+------+
正如您所看到的,該結果有2個重複項,顯然來自笛卡爾積。我想知道如何才能做到這一點,使它只輸出:
+------+------+
| code | code |
+------+------+
| 1 | 2 |
+------+------+
任何線索?謝謝!