MYSQL通过消除其他列的重复值来选择所有唯一记录

时间:2014-03-05 16:58:07

标签: mysql sql select unique

我有表名图

列是:wwid_a,wwid_b,active,date_added

值为

+--------+--------+--------+---------------------+
| wwid_a | wwid_b | active |     date_added      |
+--------+--------+--------+---------------------+
|   1943 | 402158 |      1 | 2014-03-05 09:08:51 |
|   1943 | 402209 |      1 | 2014-03-05 09:08:52 |
|   1943 | 402464 |      1 | 2014-03-05 09:08:52 |
| 402158 |   1943 |      1 | 2014-03-05 09:08:5  |
| 402209 |   1943 |      1 | 2014-03-05 09:08:59 |
| 402464 |   1943 |      1 | 2014-03-05 09:08:58 |
+--------+--------+--------+---------------------+

基本上每个条目都有重复记录,列由wwid_a和wwid_b互换。

我希望select查询能够通过备用wwid_a和wwid_b

为每个唯一记录消除重复值

类似

+--------+--------+--------+---------------------+
| wwid_a | wwid_b | active |     date_added      |
+--------+--------+--------+---------------------+
|   1943 | 402158 |      1 | 2014-03-05 09:08:51 |
|   1943 | 402209 |      1 | 2014-03-05 09:08:52 |
|   1943 | 402464 |      1 | 2014-03-05 09:08:52 |
+--------+--------+--------+---------------------+

1 个答案:

答案 0 :(得分:2)

如果确实所有内容都重复,那么这可能是删除它们的最有效方法:

select g.*
from graph g
where g.wwid_a < g.wwid_b;

如果您担心这可能总是是真的,那么您有几个选择。 not exists逻辑可能是最好的:

select g.*
from graph g
where g.wwid_a < g.wwid_b or
      not exists (select 1
                  from graph g2
                  where g2.wwid_a = g.wwid_b and
                        g2.wwid_b = g.wwid_a
                 );

即,如果第一条记录小于第二条记录,请保留一行。或者,如果匹配的小于记录不存在,请保留它。

这对于索引为graph(wwid_a, wwid_b)的较大数据最有效。