简体   繁体   English

选择列中具有多个不同值的行

[英]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.

相关问题 MySQL:选择所有行,但在列中具有指定值的行除外 - MySQL : Select all the rows except those having the specified values in columns MySQL-选择多个具有相同值的行,其中一个必须不同 - MySQL - Select multiple rows with the same values with one having to be different 选择一对在一行中具有相等值而在另一列中具有不同值的行 - Select a couple of rows having equal values in one column and different in another MySQL-用两个值相除的SELECT返回不同的行数 - MySQL - SELECT with division of two values returns different number of rows SQL选择计数>特定数量的行 - SQL select rows having count > certain number 使用联合时如何合并具有不同列数的SELECT语句? - How to combine SELECT statements having different number of columns when using union? 将多行插入一张表,每条记录的列具有不同的引用值 - INSERT multiple rows INTO one table, with each record's columns having different reference values 如何选择同一表中具有特定列中给定值的两个不同行 - How to select two different rows in same table having given values in a specific column SQL选择具有多个共同值的行 - SQL select rows having several values in common 如何在MySQL的同一张表上的两个不同输入值之间的两个不同列之间选择行 - How to select rows between two different columns with two different input values on same table on mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM