SQL:在一个表中查找一对多
有一张桌子: t1 问题是:SQL:在一个表中查找一对多,sql,Sql,有一张桌子: t1 问题是: 需要查找“Acc Status”=在线且“Acc Status”=激活的所有Id您可以在下面的查询中尝试- SELECT id FROM T1 WHERE AccStatus IN ('Online', 'Activated') GROUP BY id HAVING COUNT(DISTINCT AccStatus) = 2; 您可以尝试以下查询- SELECT id FROM T1 WHERE AccStatus IN ('Online', 'Activate
需要查找“Acc Status”=在线且“Acc Status”=激活的所有Id您可以在下面的查询中尝试-
SELECT id
FROM T1
WHERE AccStatus IN ('Online', 'Activated')
GROUP BY id
HAVING COUNT(DISTINCT AccStatus) = 2;
您可以尝试以下查询-
SELECT id
FROM T1
WHERE AccStatus IN ('Online', 'Activated')
GROUP BY id
HAVING COUNT(DISTINCT AccStatus) = 2;
尝试下面的查询
SELECT id
FROM Table_name
WHERE AccStatus = 'Online'
and id IN (SELECT id FROM Table_name wHERE AccStatus = 'Activated')
尝试下面的查询
SELECT id
FROM Table_name
WHERE AccStatus = 'Online'
and id IN (SELECT id FROM Table_name wHERE AccStatus = 'Activated')
您必须按id对
进行分组
并计算accstatus
的不同值:
select id
from t1
where accstatus in ('Online', 'Activated')
group by id
having count(distinct accstatus) = 2
如果您需要仅具有这些accstatus
值的ID:
select id
from t1
group by id
having
count(distinct accstatus) = 2
and
sum(case when accstatus not in ('Online', 'Activated') then 1 else 0 end) = 0
您必须按id对
进行分组
并计算accstatus
的不同值:
select id
from t1
where accstatus in ('Online', 'Activated')
group by id
having count(distinct accstatus) = 2
如果您需要仅具有这些accstatus
值的ID:
select id
from t1
group by id
having
count(distinct accstatus) = 2
and
sum(case when accstatus not in ('Online', 'Activated') then 1 else 0 end) = 0