![](/img/trans.png)
[英]MySQL : Select all the rows except those having the specified values in columns
[英]Select rows having number of different values in columns
假設我有一張桌子
a | b | c
--+---+--
1 | 1 | 2
1 | 2 | 3
1 | 2 | 2
3 | 2 | 3
我想選擇在此行內具有許多不同值的行。 就像是:
select a,b,c from table having size(unique(a,b,c)) = 2
因此,對於給定的數據集,這種情況下查詢返回:
a | b | c
--+---+--
1 | 1 | 2
1 | 2 | 2
3 | 2 | 3
是否有用於此類操作的mysql-operand? 顯然,我需要使用更多行和不同值作為2。
如果a,b,c是一個表的ID,則有一種整齊的方法。 然后,您可以執行子查詢並執行如下謂詞:
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.