2

我有一个t包含值的表

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+  
|    1 |    2 | x    |
|    1 |    3 | x    |
|    1 |    4 | x    |
|    2 |    1 | x    |
|    2 |    3 | x    |
|    2 |    4 | x    |
+------+------+------+

我需要这个结果:

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+  
|    1 |    2 | x    |
|    1 |    3 | x    |
|    1 |    4 | x    |
|    2 |    3 | x    |
|    2 |    4 | x    |
+------+------+------+

注 (1,2) 与 (2,1) 相同

4

1 回答 1

2

为了使用该DISTINCT子句,您需要手动对前两列进行排序:

SELECT DISTINCT
    CASE WHEN (col1 < col2) THEN col1 else col2 END AS Col1,
    CASE WHEN (col1 < col2) THEN col2 else col1 END AS Col2,
    col3
FROM t

看到这个 SQLFiddle

于 2012-09-24T07:36:20.937 回答