gpt4 book ai didi

mysql - 即使条件为真,我的sql Select IF也会返回else结果

转载 作者:行者123 更新时间:2023-11-29 22:04:15 25 4
gpt4 key购买 nike

该请求返回Else结果即使条件为真,也会显示“ELSE RESULT”。

SELECT
IF(Print_Server_Type='BW',SUM(Total_Impressions_Printed),'ELSE RESULT') AS BWTotalImpressions
FROM printedjob
WHERE
Account=''
And Job_Submission_Date>="2015-08-24"
AND Job_Submission_Date<="2015-08-28";

与 where 条件相同的请求可以正常工作

SELECT
SUM(Total_Impressions_Printed) AS BWTotalImpressions
FROM printedjob
WHERE
Account=''
And Print_Server_Type='BW'
And Job_Submission_Date>="2015-08-24"
AND Job_Submission_Date<="2015-08-28";

我使用第一个请求,因为我需要结果是选择中的一列。

最佳答案

您正在混合聚合术语(例如 SUM(Total_Impressions_Printed))和单行术语(例如 Print_Server_Type)。似乎您正在尝试针对 Print_Server_Type 的每个值返回一行,这意味着您缺少 group by 子句:

SELECT   Print_Server_Type, 
IF(Print_Server_Type = 'BW',
SUM(Total_Impressions_Printed),
'ELSE RESULT') AS BWTotalImpressions
FROM printedjob
WHERE Account = '' AND
Job_Submission_Date >= "2015-08-24" AND
Job_Submission_Date <= "2015-08-28"
GROUP BY Print_Server_Type

关于mysql - 即使条件为真,我的sql Select IF也会返回else结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32315977/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com