请教各位前辈几个关于SQL语句我所疑惑的问题,今天弄了一个下午,都绕晕头了!麻烦帮我看看!
personnelinfo表和borninfo表(两表与主键autoid关联)
Select
(
SELECT count(p.NowUnitCoding) FROM dbo.wm_BornInfo b RIGHT OUTER JOIN dbo.wm_PersonnelInfo p ON b.PersonnelAutoID = p.PersonnelAutoID
WHERE (b.NowIsFeed = 0) AND (p.NowUnitCoding LIKE '010% ')
--这里我想统计的是p表有该记录存在,b表该记录也存在,
--条件:在b表中该条记录的NowIsFeed 字段值为0 ,p表中NowUnitCoding包含010
)
+
(
SELECT count(p.NowUnitCoding) FROM dbo.wm_PersonnelInfo p
WHERE p.PersonnelAutoID not IN (SELECT b.personnelautoid FROM wm_BornInfo b WHERE p.NowUnitCoding LIKE '010% ')
--这里我想统计的是:在p表中存在该条记录,但是在b表中不存在该记录
--条件:p表中NowUnitCoding包含010
--关键我疑惑的在这里,如后描述
)
as CountBornNowNotFeed
--这里将两种情况相加得出我要的最后结果.
我不清楚第2个count里的条件:
SELECT count(p.NowUnitCoding) FROM dbo.wm_PersonnelInfo p
WHERE p.PersonnelAutoID not IN (SELECT b.personnelautoid FROM wm_BornInfo b WHERE p.NowUnitCoding LIKE '010% '
如果我改为
=======================>
SELECT count(p.NowUnitCoding) FROM dbo.wm_PersonnelInfo p
WHERE p.PersonnelAutoID not IN (SELECT b.personnelautoid FROM wm_BornInfo b ) and p.NowUnitCoding LIKE '010% '
这样的统计出来的总的记录数字(统计出来的结果的确有很大的数字上的差异),我不清楚语句上有什么差别,具体能给我解释一下吗?我只是将我最疑惑的部分语句单独提出来请教大家,麻烦大家帮帮我!头都晕了!
------解决方案--------------------SELECT count(p.NowUnitCoding) FROM dbo.wm_PersonnelInfo p
WHERE p.PersonnelAutoID not IN
--注,以下是一个主体
(SELECT b.personnelautoid FROM wm_BornInfo b WHERE p.NowUnitCoding LIKE '010% ')
SELECT count(p.NowUnitCoding) FROM dbo.wm_PersonnelInfo p
WHERE p.PersonnelAutoID not IN (SELECT b.personnelautoid FROM wm_BornInfo b ) and p.NowUnitCoding LIKE '010% '
注,以下这句(SELECT b.personnelautoid FROM wm_BornInfo b )是一个主体,后面的and是与上一个条件,所以两种写法区别大大的呀.