简体   繁体   English

MySQL选择两列之间的唯一值的行

[英]MySql select rows of unique values between two columns

I can't seem to get this to work. 我似乎无法使它正常工作。

I have this table: 我有这张桌子:

A  B
- - -
1  2
1  3
1  4
4  1
3  1
2  1

I just want the first three rows, 我只想要前三行,

A  B
- - -
1  2
1  3
1  4

Because the last 3 rows are merely the opposite of the first three. 因为后三行仅与前三行相反。

How can I do this with a MySQL Query? 如何使用MySQL查询做到这一点?

Please help! 请帮忙!

SQL Fiddle SQL小提琴

Given this schema, 有了这个架构,

CREATE TABLE t
    (`A` int, `B` int)
;

INSERT INTO t
    (`A`, `B`)
VALUES
    (1, 2),
    (1, 3),
    (1, 4),
    (4, 1),
    (3, 1),
    (2, 1)
;

You can use this query to take your a,b data, reduce it, and eliminate the duplicates. 您可以使用此查询获取a,b数据,减少数据并消除重复项。

SELECT DISTINCT LEAST(A,B) AS A,
                GREATEST(A,B) AS B
  FROM t

Results : 结果

| A | B |
|---|---|
| 1 | 2 |
| 1 | 3 |
| 1 | 4 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM