我有两张桌子。客户(clientid,公司,州,地位)和supportstatus(clientid,公司,地位,协议)。以下查询是否应该返回协议值为0且状态不等于“已禁用”的所有公司? 我很困惑,因为我正在选择公司,我正在加入,两个表由公共因子(clientid),我正在过滤查询的结果。
SELECT cl.company
FROM clients cl
INNER JOIN supportstatus su
ON cl.clientid = su.clientid
WHERE su.agreement11 = 0
AND su.status <> 'disabled'
ORDER BY cl.company
ColdFusion来源
<cfquery name="qryPendingAgreement" datasource="support">
SELECT clientid
FROM supportstatus
WHERE agreement11 = 0 AND status <> 'disabled'
</cfquery>
<cfquery name="qryClient" datasource="support">
SELECT clientid, company, state, serv_billing
FROM clients
WHERE prod_arth = 1 OR prod_artr = 1 OR prod_epcr_host = 1 OR prod_epcr_remote = 1 OR prod_billing = 1 OR prod_collections = 1
</cfquery>
<cfquery name="qryResults" dbtype="query">
SELECT qryClient.company, qryClient.state, qryClient.serv_billing
FROM qryPendingAgreement, qryClient
WHERE qryPendingAgreement.clientid = qryClient.clientid
order by qryClient.company
</cfquery>
答案 0 :(得分:1)
您没有说明查询给您的结果。如果查询过滤的行数超出您的预期,那么答案可能是三值逻辑。
null != 'disabled'
- &gt; null
,这不是真的,行会被过滤。
请尝试使用此过滤器。
WHERE su.agreement11 = 0
AND isnull(su.status,'') <> 'disabled'
答案 1 :(得分:0)
这可以帮助您测试查询。你的问题不够具体,所以,这是我能做的最好的事情:)
DECLARE @client TABLE
(
clientid int,
company VARCHAR(50)
)
DECLARE @supportstatus TABLE
(
clientid int,
[status] VARCHAR(50),
agreement INT
)
INSERT INTO @client ([clientid],[company]) VALUES ( 0,'acme' )
INSERT INTO @client ([clientid],[company]) VALUES ( 1,'byron' )
INSERT INTO @client ([clientid],[company]) VALUES ( 2,'cathode' )
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 0, 'disabled',0)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 0, 'disabled',1)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 0, 'somethingelse',0)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 0, 'somethingelse',1)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 1, 'disabled',0)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 1, 'disabled',1)
--INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 1, 'somethingelse',0)
--INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 1, 'somethingelse',1)
--INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 2, 'disabled',0)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 2, 'disabled',1)
--INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 2, 'somethingelse',0)
INSERT INTO @supportstatus ([clientid],[status], agreement) VALUES ( 2, 'somethingelse',1)
-- all companies who have su.agreement = 0 and su.status <> 'disabled'
SELECT cl.company
FROM @client cl
JOIN @supportstatus su ON cl.clientid = su.clientid
WHERE su.agreement = 0 AND su.status <> 'disabled'
Order By cl.company
答案 2 :(得分:0)
这是一个疯狂的猜测,但如果您使用的是此版本:
SELECT cl.company
FROM clients cl
INNER JOIN supportstatus su
ON cl.clientid = su.clientid
WHERE su.agreement11 = 0
AND su.status <> 'disabled'
OR prod_arth = 1 OR prod_artr = 1
OR prod_epcr_host = 1 OR prod_epcr_remote = 1
OR prod_billing = 1 OR prod_collections = 1
ORDER BY cl.company
问题是AND
,OR
优先级。试试这个:
SELECT cl.company
FROM clients cl
INNER JOIN supportstatus su
ON cl.clientid = su.clientid
WHERE su.agreement11 = 0
AND su.status <> 'disabled'
AND ( prod_arth = 1 OR prod_artr = 1
OR prod_epcr_host = 1 OR prod_epcr_remote = 1
OR prod_billing = 1 OR prod_collections = 1
)
ORDER BY cl.company