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

时间:2015-08-31 16:40:28

标签: mysql

此请求返回Else结果 即使条件成立,也会“结果”。

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"; 

我正在使用第一个请求,因为我需要将结果作为select的列。

3 个答案:

答案 0 :(得分:0)

您正在混合聚合术语(如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

答案 1 :(得分:0)

我认为您的ELSE语句写得不正确。 else不应该是引号'ELSE RESULT'......我首先要验证格式是否正确以编写查询。

答案 2 :(得分:0)

修正了问题,这是请求:

sudo port -f uninstall libjpeg-turbo
sudo port install jpeg
sudo port rev-upgrade