简体   繁体   English

获得两列的独特组合

[英]Getting distinct combination of two columns

Using Hibernate, I want to get the rows of values such that: 使用Hibernate,我想获取值的行,例如:

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? 我可以在Hibernate上解决这个问题吗? Or can anyone provide a MySQL implementation of this. 或者任何人都可以提供对此的MySQL实现。 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. 您可以使用mysql minimum ()great()运算符来确保较小的数字排在前,最大的数字排在后。 This way you can use distinct to eliminate duplicates: 这样,您可以使用distinct来消除重复项:

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); 从(col1 + col2)的表组中选择*;

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

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