我有以下两个查询:
select count(*) from table_1 where c_id=12345
select count(*) from table_2 where success=1 and c_id=12345
我将这两个计数添加到C#层上。然而,table_1
与table_2
这两个有一个名为列m_id
。我希望能够获得table_2
同时满足以下两个条件的总数:
success=1 and c_id=12345
m_id
不等于m_id
来自的任何值table_1 where c_id=12345
。这可能吗?是否可以在单个查询中完成?我还是SQL的新手,如果这是一个明显的问题,我深表歉意。
只需not exists
在第二个查询中添加一个子句:
select count(*)
from table_2 t2
where t2.success = 1 and t2.c_id = 12345 and
not exists (select 1 from table_1 t1 where t1.m_id = t2.mid and t1.c_id = 12345);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句