使用CASE语法计算列值

时间:2018-12-09 20:52:06

标签: mysql

我很困惑,绝对不确定这是否正确。 在下面的示例中,我尝试检查促销类型是1(百分比为10%)还是2(硬价格为10 EUR),然后计算促销价格,仅在Worksheet时计算价格。否则价格保持不变。

main_product_id IS NOT NULL

我得到的错误是:

SELECT p.price
FROM product as p
LEFT JOIN promotion_product as pp ON p.id=pp.main_product_id 
LEFT JOIN promotion as pr ON pp.promo_id=pr.id
(
CASE
    WHEN pp.main_product_id IS NOT NULL THEN 
        CASE 
            WHEN pr.type=1 THEN p.price = p.price - (p.price * pr.value/100)
            WHEN pr.type=2 THEN p.price = p.price - pr.value 
        END
END
)

这很清楚,我知道。但是如何计算新价格,是否可以使用#1305 - FUNCTION pr.id does not exist 语法?

产品表:

CASE

促销表:

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| category_id    | int(11)      | YES  | MUL | NULL    |                |
| brand_id       | int(11)      | YES  | MUL | NULL    |                |
| sort           | int(11)      | NO   |     | 999     |                |
| enable         | tinyint(1)   | NO   | MUL | 2       |                |
| product_number | varchar(255) | YES  | MUL | NULL    |                |
| price          | float        | YES  |     | NULL    |                |
| quantity       | float        | YES  |     | NULL    |                |
| rating         | tinyint(4)   | NO   |     | 0       |                |
+----------------+--------------+------+-----+---------+----------------+

促销产品表:

+------------+------------+------+-----+---------+----------------+
| Field      | Type       | Null | Key | Default | Extra          |
+------------+------------+------+-----+---------+----------------+
| id         | int(11)    | NO   | PRI | NULL    | auto_increment |
| start_date | timestamp  | YES  |     | NULL    |                |
| end_date   | timestamp  | YES  |     | NULL    |                |
| type       | tinyint(4) | NO   |     | NULL    |                |
| value      | float      | NO   |     | NULL    |                |
| enable     | tinyint(4) | NO   |     | 2       |                |
+------------+------------+------+-----+---------+----------------+

2 个答案:

答案 0 :(得分:1)

这是我实现您目标的方式:

SELECT
    IF(pp.main_product_id IS NOT NULL,
       CASE
           WHEN pr.type = 1 THEN p.price - (p.price * pr.value / 100)
           WHEN pr.type = 2 THEN p.price - pr.value
           ELSE p.price -- Added a default case.
       END,
       p.price) AS finalPrice
FROM
    product AS p
LEFT JOIN
    promotion_product AS pp ON p.id = pp.main_product_id 
LEFT JOIN
    promotion AS pr ON pp.promo_id = pr.id

还请注意,start_date表上有end_datepromotion当前正在忽略。

答案 1 :(得分:1)

CASE...WHEN...THEN表达式用于SELECT子句中。查询末尾用括号括起来。因为您的查询看起来像pr.id(yadda yadda),MySQL认为pr.id应该是一个函数。不是,所以MySQL会抛出错误。

尝试这样的事情:

SELECT p.price, 
           CASE
               WHEN pr.type=1 THEN p.price = p.price - (p.price * pr.value/100)
               WHEN pr.type=2 THEN p.price = p.price - pr.value 
               ELSE p.price
           END promoted_price
  FROM product as p
  LEFT JOIN promotion_product as pp ON p.id=pp.main_product_id 
  LEFT JOIN promotion as pr ON pp.promo_id=pr.id

我重构了您的case表达式,因此它没有嵌套。 ELSE子句处理默认情况下的情况,其中pr.type不是12,并且左联接的ON子句不匹配任何内容。