简体   繁体   中英

Getting distinct combination of two columns

Using Hibernate, I want to get the rows of values such that:

col1   | col2
-------+-------
   1   |    2
-------+-------
   2   |    1
-------+-------
   3   |    4
-------+-------
   4   |    5
-------+-------
   4   |    3

will produce:

col1   | col2
-------+-------
   1   |    2
-------+-------
   3   |    4
-------+-------
   4   |    5

Can I get this out in Hibernate on grails? Or can anyone provide a MySQL implementation of this. Been battling this long enough.

You can use mysql least() and greatest() operators to make sure that the smaller number comes first and the highest comes later. This way you can use distinct to eliminate duplicates:

select distinct least(col1, fol2) as col1, greatest(col1, col2) as col2
from yourtable

You can group the two column like :

select * from yourtable group by (col1+col2);

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