两列不同

时间:2017-06-06 16:01:43

标签: mysql

如何从两列中选择唯一结果:

+---------+--------------+
| team_id | opponents_id |
+---------+--------------+
|       1 |            2 |
|       1 |            3 |
|       1 |            4 |
|       1 |            5 |
|       1 |            6 |
|       2 |            1 |
|       2 |            3 |
|       2 |            4 |
|       2 |            5 |
|       2 |            6 |
|       3 |            1 |
|       3 |            2 |
|       3 |            4 |
|       3 |            5 |
|       3 |            6 |
|       4 |            1 |
|       4 |            2 |
|       4 |            3 |
|       4 |            5 |
|       4 |            6 |
|       5 |            1 |
|       5 |            2 |
|       5 |            3 |
|       5 |            4 |
|       5 |            6 |
|       6 |            1 |
|       6 |            2 |
|       6 |            3 |
|       6 |            4 |
|       6 |            5 |
+---------+--------------+

2 个答案:

答案 0 :(得分:2)

只需使用不同的

 select distinct team_id,opponents_id
   from your_table

答案 1 :(得分:1)

如果要根据两列抑制重复,则应使用复合索引column1,column2

对值进行分组
SELECT column1, column2
FROM MA_TABLE
GROUP BY column1, column2

它应该返回一个唯一结果集列表