简体   繁体   English

MySQL 查询未提供所需的输出

[英]MySQL query not giving desired output

I have a MySQL query:我有一个 MySQL 查询:

SELECT DATE_FORMAT(pickupdatetime, '%m/%y') AS mmyy,
SUM(CASE WHEN (status = 'Canceled' or 'Unconfirmed' or 'Communicating') and (class = 'local') THEN quotedprice ELSE 0 END) AS potentiallocal,
SUM(CASE WHEN (status = 'Closed' or 'Confirmed' or 'Open') and (class = 'local') THEN quotedprice ELSE 0 END) AS actuallocal,

SUM(CASE WHEN (status = 'Canceled' or 'Unconfirmed' or 'Communicating') and (class = 'travel') THEN quotedprice ELSE 0 END) AS potentialtravel,
SUM(CASE WHEN (status = 'Closed' or 'Confirmed' or 'Open') and (class = 'travel') THEN quotedprice ELSE 0 END) AS actualtravel,

SUM(CASE WHEN (status = 'Canceled' or 'Unconfirmed' or 'Communicating') and (class = 'suv') THEN quotedprice ELSE 0 END) AS potentialsuv,
SUM(CASE WHEN (status = 'Closed' or 'Confirmed' or 'Open') and (class = 'suv') THEN quotedprice ELSE 0 END) AS actualsuv,

SUM(CASE WHEN (status = 'Canceled' or 'Unconfirmed' or 'Communicating') and (class = 'minivan') THEN quotedprice ELSE 0 END) AS potentialminivan,
SUM(CASE WHEN (status = 'Closed' or 'Confirmed' or 'Open') and (class = 'minivan') THEN quotedprice ELSE 0 END) AS actualminivan

FROM `reservations`
WHERE pickuploc = 'la'
GROUP BY DATE_FORMAT(pickupdatetime, '%m/%y'), YEAR(pickupdatetime)
ORDER BY YEAR(pickupdatetime), DATE_FORMAT(pickupdatetime, '%m/%y')

I want to get the combined values of quotedprice for status Canceled , Unconfirmed , and Communicating for local class and then the combined values of quotedprice for status Closed or Confirmed or Open for local class.我想要得到的组合值quotedpricestatus CanceledUnconfirmedCommunicatinglocal类,然后组合值quotedpricestatus ClosedConfirmedOpenlocal类。 I am trying to do this again for each class (local, travel, suv, minivan) but it is not giving the correct output, what am I doing wrong?我正在尝试为每个班级(本地、旅行、SUV、小型货车)再次执行此操作,但没有给出正确的输出,我做错了什么?

All the expressions like:所有表达式如:

WHEN (status = 'Canceled' or 'Unconfirmed' or 'Communicating')

are wrong, because they are equivalent to:是错误的,因为它们等价于:

WHEN (status = 'Canceled' or 0 or 0)

and finally equivalent to just:最后相当于:

WHEN (status = 'Canceled')

because the strings 'Unconfirmed' and 'Communicating' are implicitly converted to integers since they are operands of a boolean expression.因为字符串'Unconfirmed''Communicating'被隐式转换为整数,因为它们是布尔表达式的操作数。
What you should do is change the expressions like:您应该做的是更改表达式,例如:

WHEN (status = 'Canceled' or status = 'Unconfirmed' or status = 'Communicating')

or:或者:

WHEN (status in ('Canceled', 'Unconfirmed', 'Communicating'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM