简体   繁体   English

使用CASE语法计算列值

[英]Compute column value with CASE syntax

I am pretty confused and absolutely not sure if this is the right way. 我很困惑,绝对不确定这是否正确。 In the example below I am trying to check if the promotion type is 1 ( percentage eg 10% ) or 2 ( hard price eg 10 EUR ) and compute the price after it and that only if main_product_id IS NOT NULL . 在下面的示例中,我尝试检查促销类型是1(百分比,例如10%)还是2(硬价格,例如10 EUR),然后计算促销价格,并且仅当main_product_id IS NOT NULL时才计算。 Otherwise the price stays the same. 否则价格保持不变。

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
)

What I get as error is: 我得到的错误是:

#1305 - FUNCTION pr.id does not exist

This is pretty clear I know. 我知道这很清楚。 But how to compute the new price and is it possible with CASE syntax ? 但是如何计算新价格,是否可以使用CASE语法?

product table : 产品表:

+----------------+--------------+------+-----+---------+----------------+
| 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       |                |
+----------------+--------------+------+-----+---------+----------------+

promotion table: 促销表:

+------------+------------+------+-----+---------+----------------+
| 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       |                |
+------------+------------+------+-----+---------+----------------+

promotion_product table: Promotion_product表:

+-----------------+---------+------+-----+---------+----------------+
| Field           | Type    | Null | Key | Default | Extra          |
+-----------------+---------+------+-----+---------+----------------+
| id              | int(11) | NO   | PRI | NULL    | auto_increment |
| promo_id        | int(11) | NO   | MUL | NULL    |                |
| product_id      | int(11) | YES  | MUL | NULL    |                |
| main_product_id | int(11) | YES  | MUL | NULL    |                |
+-----------------+---------+------+-----+---------+----------------+

This is the way I will approach your goal: 这是我将达到您的目标的方式:

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

Note also, that you have start_date and end_date on your promotion table that you are currently ignoring. 另请注意,您正在忽略的promotion表上有开始start_dateend_date日期。

CASE...WHEN...THEN expressions are for use in SELECT clauses. CASE...WHEN...THEN表达式在SELECT子句中使用。 You have yours wrapped in parentheses at the end of your query. 查询末尾用括号括起来。 Because your query looks like pr.id(yadda yadda) MySQL thinks pr.id should be a function. 因为您的查询看起来像pr.id(yadda yadda) MySQL认为pr.id应该是一个函数。 It isn't, so MySQL throws your error. 事实并非如此,因此MySQL会引发您的错误。

Try something like this: 尝试这样的事情:

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

I refactored your case expressions so it's not nested. 我重构了您的case表达式,使其不嵌套。 The ELSE clause deals with the default cases where pr.type isn't 1 or 2 , and where the ON clauses of your left joins don't match anything. ELSE子句处理的默认情况是pr.type不是12 ,并且左联接的ON子句不匹配任何东西。

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

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