Mysql查询错误WHEN语句

时间:2014-02-14 15:35:28

标签: mysql sql

我在查询WHEN语句中使用

SELECT SQL_CALC_FOUND_ROWS `shop_items`.*, 
IF (`shop_items`.`shop_currency_id` = '3', 
    CASE 
        WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 
            THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 23.639664 / 100 
        WHEN `shop_specialprices`.`min_quantity` <= 1 
            THEN `shop_specialprices`.`price` 
        WHEN `shop_discounts`.`percent` > 0     
            THEN `price` * (100 - `shop_discounts`.`percent`) * 23.639664 / 100 
        ELSE `price`, 
    IF (`shop_items`.`shop_currency_id` = '2', 
        CASE 
            WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 
                THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 36.889479 / 100 
            WHEN `shop_specialprices`.`min_quantity` <= 1 
                THEN `shop_specialprices`.`price` 
            WHEN `shop_discounts`.`percent` > 0 
                THEN `price` * (100 - `shop_discounts`.`percent`) * 36.889479 / 100 
            ELSE `price`, 
        IF (`shop_items`.`shop_currency_id` = '1', 
            CASE 
                WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 
                    THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 1 / 100 
                WHEN `shop_specialprices`.`min_quantity` <= 1 
                    THEN `shop_specialprices`.`price` 
                WHEN `shop_discounts`.`percent` > 0 
                    THEN `price` * (100 - `shop_discounts`.`percent`) * 1 / 100 
                ELSE `price`, price
        )
    )
) AS `price` 
FROM `shop_items` 
LEFT OUTER JOIN `shop_item_discounts` 
ON `shop_items`.`id` = `shop_item_discounts`.`shop_item_id` 
LEFT OUTER JOIN `shop_specialprices` 
ON `shop_items`.`id` = `shop_specialprices`.`shop_item_id` 
AND ( `shop_specialprices`.`min_quantity` <= '1' 
LEFT OUTER JOIN `shop_discounts` ON `shop_item_discounts`.`shop_discount_id` = `shop_discounts`.`id` 
AND ( `shop_discounts`.`end_datetime` >= '2014-02-14 19:27:18' 
OR `shop_discounts`.`end_datetime` = '0000-00-00 00:00:00' 
AND `shop_discounts`.`start_datetime` <= '2014-02-14 19:27:18' ) 
WHERE `shop_items`.`shop_id` = '1' 
AND `shop_items`.`active` = 1 
AND ( `shop_items`.`start_datetime` < '2014-02-14 19:27:17' 
OR `shop_items`.`start_datetime` = '0000-00-00 00:00:00' ) 
AND ( `shop_items`.`end_datetime` > '2014-02-14 19:27:17' 
OR `shop_items`.`end_datetime` = '0000-00-00 00:00:00' ) 
AND `shop_items`.`siteuser_group_id` IN (0, -1) 
AND `shop_items`.`modification_id` = 0 
AND `shop_items`.`shop_group_id` IN ('688', '634', '635', '642', '638', '707', '706', '632') 
AND `shop_items`.`deleted` = 0 
HAVING `price` >= 5000 
AND `price` <= 10000 
ORDER BY `price` ASC 
LIMIT 6

需要发现错误

错误

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' IF (`shop_items`.`shop_currency_id` = '2', CASE WHEN `shop_specialprices`.`min_' at line 1

2 个答案:

答案 0 :(得分:1)

你忘了END,

   ELSE `price` END, IF(`shop_items`.`shop_currency_id` = '2',......
                ***-----you forgot this here

这里是工作查询

  SELECT SQL_CALC_FOUND_ROWS `shop_items`.*, IF (`shop_items`.`shop_currency_id` = '3',
                                            CASE WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 23.639664 / 100 
                                                 WHEN `shop_specialprices`.`min_quantity` <= 1 THEN `shop_specialprices`.`price` 
                                                 WHEN `shop_discounts`.`percent` > 0 THEN `price` * (100 - `shop_discounts`.`percent`) * 23.639664 / 100 
                                                 ELSE `price` end, 
                                                IF (`shop_items`.`shop_currency_id` = '2', CASE WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 36.889479 / 100 
                                                                                                WHEN `shop_specialprices`.`min_quantity` <= 1 THEN `shop_specialprices`.`price` 
                                                                                                WHEN `shop_discounts`.`percent` > 0 THEN `price` * (100 - `shop_discounts`.`percent`) * 36.889479 / 100 
                                                                                                ELSE `price` END, 
                                                                                                IF (`shop_items`.`shop_currency_id` = '1', CASE WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 1 / 100 
                                                                                                                                                WHEN `shop_specialprices`.`min_quantity` <= 1 THEN `shop_specialprices`.`price` 
                                                                                                                                                WHEN `shop_discounts`.`percent` > 0 THEN `price` * (100 - `shop_discounts`.`percent`) * 1 / 100 
                                                                                                                                                ELSE `price` END, price))) AS `price` 
 FROM `shop_items` 
 LEFT OUTER JOIN `shop_item_discounts` ON `shop_items`.`id` = `shop_item_discounts`.`shop_item_id` 
 LEFT OUTER JOIN `shop_specialprices` ON `shop_items`.`id` = `shop_specialprices`.`shop_item_id` 
  AND ( `shop_specialprices`.`min_quantity` <= '1' )
 LEFT OUTER JOIN `shop_discounts` ON `shop_item_discounts`.`shop_discount_id` = `shop_discounts`.`id` 
  AND ( `shop_discounts`.`end_datetime` >= '2014-02-14 19:27:18' OR `shop_discounts`.`end_datetime` = '0000-00-00 00:00:00' AND `shop_discounts`.`start_datetime` <= '2014-02-14 19:27:18' ) 
  WHERE `shop_items`.`shop_id` = '1' 
  AND `shop_items`.`active` = 1 
  AND ( `shop_items`.`start_datetime` < '2014-02-14 19:27:17' OR `shop_items`.`start_datetime` = '0000-00-00 00:00:00' ) 
  AND ( `shop_items`.`end_datetime` > '2014-02-14 19:27:17' OR `shop_items`.`end_datetime` = '0000-00-00 00:00:00' ) 
  AND `shop_items`.`siteuser_group_id` IN (0, -1) 
  AND `shop_items`.`modification_id` = 0 
  AND `shop_items`.`shop_group_id` IN ('688', '634', '635', '642', '638', '707', '706', '632') 
  AND `shop_items`.`deleted` = 0 
  HAVING `price` >= 5000 AND `price` <= 10000 ORDER BY `price` ASC LIMIT 6
  HAVING `price` >= 5000 AND `price` <= 10000 ORDER BY `price` ASC LIMIT 6

答案 1 :(得分:0)

第二次和第三次连接之间的ON和OUTER JOIN出错。在开始新的连接之前,您应该关闭ON条件的括号。

这是一个更加格式化的SQL:

SELECT
    SQL_CALC_FOUND_ROWS `shop_items`.*,
    IF(`shop_items`.`shop_currency_id` = '3', CASE WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 23.639664 / 100 WHEN `shop_specialprices`.`min_quantity` <= 1 THEN `shop_specialprices`.`price` WHEN `shop_discounts`.`percent` > 0 THEN `price` * (100 - `shop_discounts`.`percent`) * 23.639664 / 100 ELSE `price`, IF (`shop_items`.`shop_currency_id` = '2', CASE WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 36.889479 / 100 WHEN `shop_specialprices`.`min_quantity` <= 1 THEN `shop_specialprices`.`price` WHEN `shop_discounts`.`percent` > 0 THEN `price` * (100 - `shop_discounts`.`percent`) * 36.889479 / 100 ELSE `price`, IF (`shop_items`.`shop_currency_id` = '1', CASE WHEN `shop_specialprices`.`min_quantity` <= 1 AND `shop_discounts`.`percent` > 0 THEN `shop_specialprices`.`price` * (100 - `shop_discounts`.`percent`) * 1 / 100 WHEN `shop_specialprices`.`min_quantity` <= 1 THEN `shop_specialprices`.`price` WHEN `shop_discounts`.`percent` > 0 THEN `price` * (100 - `shop_discounts`.`percent`) * 1 / 100 ELSE `price`, price))) AS `price`
FROM `shop_items`
LEFT OUTER JOIN `shop_item_discounts`
    ON `shop_items`.`id` = `shop_item_discounts`.`shop_item_id`
LEFT OUTER JOIN `shop_specialprices`
    ON `shop_items`.`id` = `shop_specialprices`.`shop_item_id`
    AND `shop_specialprices`.`min_quantity` <= '1'
LEFT OUTER JOIN `shop_discounts`
    ON `shop_item_discounts`.`shop_discount_id` = `shop_discounts`.`id`
    AND (
        `shop_discounts`.`end_datetime` >= '2014-02-14 19:27:18'
        OR `shop_discounts`.`end_datetime` = '0000-00-00 00:00:00'
        AND `shop_discounts`.`start_datetime` <= '2014-02-14 19:27:18'
    )
WHERE
    `shop_items`.`shop_id` = '1'
    AND `shop_items`.`active` = 1
    AND (
        `shop_items`.`start_datetime` < '2014-02-14 19:27:17'
        OR `shop_items`.`start_datetime` = '0000-00-00 00:00:00'
    )
    AND (
        `shop_items`.`end_datetime` > '2014-02-14 19:27:17'
        OR `shop_items`.`end_datetime` = '0000-00-00 00:00:00'
    )
    AND `shop_items`.`siteuser_group_id` IN (0, -1)
    AND `shop_items`.`modification_id` = 0
    AND `shop_items`.`shop_group_id` IN ('688', '634', '635', '642', '638', '707', '706', '632')
    AND `shop_items`.`deleted` = 0
HAVING
    `price` >= 5000
    AND `price` <= 10000
ORDER BY `price` ASC LIMIT 6