简体   繁体   中英

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?

Please help!

SQL Fiddle

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.

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

Results :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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