我有两张表tblorders
和tblprogramme
,如下图所示。
我将从asp.net的下拉列表中选择PartyName
(来自tblorders
),因为只有那些JobNo应该来自Status
(来自tblprogramme
)被派遣。
我想要查询。
e.g
我想要" Parry"(在表tblorders中)" JobNo"所有"状态"(在tblprogramme中)是"派遣")
在我的情况下来自下表图像
查询应返回" JobNo"(2)。作为Parry的这个JobNo(在tblProgramme"中)具有状态(Dispatched ib tblprogramme
答案 0 :(得分:2)
SELECT t1.JobNo
FROM tblOrders t1
WHERE t1.PartyName = 'Parry'
AND NOT EXISTS ( SELECT *
FROM tblProgramme t2
WHERE t1.JobNo = t2.JobNo
AND t2.ProgrammeStatus <> 'Dispatched' );
答案 1 :(得分:1)
尝试以下,
SELECT *
FROM (
SELECT
jobno,
name,
TotalCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno
),
DispatchedCount = (
SELECT COUNT(*)
FROM tblProgramme
WHERE jobno = o.jobno AND status = 'dispatched'
)
FROM tblOrder o
) t
WHERE name = 'Parry'
AND TotalCount = DispatchedCount