簡體   English   中英

如何找到具有特定列的所有值的設備?

[英]How to find devices that have all values of specific column?

我有一張這樣的桌子;

deviceId, status, time

狀態欄可能只有active, idle, alarm 我想找到具有所有狀態值的所有 deviceId。

因此,如果一個設備在該表的狀態列中有active, idle and alarm值,我想要它的 id。

我怎樣才能做到這一點?

您可以使用group byhaving 如果您知道存在三個值,請執行以下操作:

select deviceId
from t
group by deviceId
having count(distinct status) = 3;

如果您不知道列中值的數量,請使用子查詢來計算它們:

select deviceId
from t
group by deviceId
having count(distinct status) = (select count(distinct status) from t);

如果您特別想要這三個,請添加where子句:

select deviceId
from t
where status in ('active', 'idle', 'alarm')
group by deviceId
having count(distinct status) = 3;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM