SQL过滤查询

时间:2019-04-15 17:09:37

标签: sql

我试图弄清楚这个查询,但需要一些帮助。

编辑了帖子,认为我需要清除问题。 我需要从T2筛选出行,其中将属于特定AcctId的BoxNums的所有行的Status设置为55,并将该AcctId = Y的行设置为Auto

enter image description here

enter image description here

enter image description here

4 个答案:

答案 0 :(得分:0)

您可以进行聚合:

select accountid
from table t
group by accountid
having min(status) = max(status) and min(status) = 55;

对于所有列,您都可以执行JOIN或使用NOT EXISTS

select t2.*
from t1 inner join
     t2
     on t2.accountid = t1.accountid
where t1.auto = 'Y' and
      t2.status = 55 and
      not exists (select 1 from table t11 where t2.accountid = t11.accountid and t11.status <> t2.status);

答案 1 :(得分:0)

如果要查看原始数据,请使用not exists

select t.*
from t1 join
     t2
     on t1.accountid = t2.accountid
where t1.auto = 'Y' and
      not exists (select 1
                  from t t2
                  where t2.accountid = t.accountid and t2.status <> 55
                 );

答案 2 :(得分:0)

这是您想要的吗?

这是一种简单的subquery用法,它为存在于T2中并且在T1中也具有值T2的特定帐户id提供数据Y

      SELECT  Account_Id, 
      Box_num,Status FROM T2 WHERE 
       STATUS 
      =55 and account_id in (SELECT 
        account_id from T1 where auto='Y')

答案 3 :(得分:0)

这将起作用:

select b.* from 
t1 a,t2 b where
a.account_id=b.account_id and
a.auto='Y' and b.status=55;

检查https://www.db-fiddle.com/f/wErgQqhTvLRo4JFi81YX4V/0