ID Foreign_ID Number Status_Id
1 101 xyz11 3
2 101 xyz22 3
3 201 xyz33 3
4 201 xyz44 1
5 201 xyz55 7
6 301 xyz66 1
7 301 xyz77 7
8 301 xyz88 7Status_Id,3表示活动,1表示已取消,7表示已撤消我要选择只有status_id 1或7且不应该有任何活动行的行。查询应该只返回带有301 (Foreign_ID)的行。
发布于 2018-05-15 00:13:46
您可以尝试以下代码:
Select *
From Yourtablename
Where Status_Id in (1, 7)
AND Foreign_ID = 301;发布于 2018-05-15 00:13:56
使用not exists:
select *
from table t
where not exists (
select 1 from table where Foreign_ID = t.Foreign_ID and Status_Id = 3
);这称为关联subquery,其中subquery依赖于外部查询,如果外部查询表有3行,则subquery将执行3次。
在您的示例中,子查询具有filter with status_id = 3,这意味着它将只选择status_id =3的行,依此类推,外部查询具有NOT EXISTS筛选器,这意味着它将选择status_id <> 3的行。
发布于 2018-05-15 00:14:36
我必须承认,也许我还没有完全理解你的需求,但这应该是可行的:
SELECT * FROM table WHERE (Status_Id=1 OR Status_Id=7) AND Foreign_ID=301https://stackoverflow.com/questions/50334747
复制相似问题