[英]Select rows having number of different values in columns
Assuming I have a table like 假设我有一张桌子
a | b | c
--+---+--
1 | 1 | 2
1 | 2 | 3
1 | 2 | 2
3 | 2 | 3
I want to select rows, which have a number of different values WITHIN this row. 我想选择在此行内具有许多不同值的行。 Something like:
就像是:
select a,b,c from table having size(unique(a,b,c)) = 2
So for the given set of data, this case the query returns: 因此,对于给定的数据集,这种情况下查询返回:
a | b | c
--+---+--
1 | 1 | 2
1 | 2 | 2
3 | 2 | 3
Is there a mysql-operand for this kind of thing? 是否有用于此类操作的mysql-operand? Obviously I need to use this with more rows and different values as 2.
显然,我需要使用更多行和不同值作为2。
There is a neat way of doing this, in case of a,b,c are id's of one table. 如果a,b,c是一个表的ID,则有一种整齐的方法。 Then you can do a subquery and do an in predicate like this:
然后,您可以执行子查询并执行如下谓词:
select
a,b,c,
(select count(distinct(*)) from table where id in (a,b,c)) number
from table
having number = x
select a,b,c from table WHERE a != b AND b != c AND a != c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.