Mysql查询语句时出错
我在查询WHEN语句中使用Mysql查询语句时出错,mysql,sql,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`.`pri
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
第二次和第三次连接之间的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
选择
SQL\u CALC\u找到了\u行'shop\u items`.*,
如果(`shop\u items`.`shop\u currency\u id`='3',当`shop\u specialprice`.`min\u quantity`0,则`shop\u specialprice`.`price`.`price`*(100-`shop\u折扣`.`percent`)`23.639664/100,当`shop\u specialprice`.`min\u数量`0,则`price`.`100-`shop\u折扣`percent`.`23.639664/100,如果(`shop\u items`.`shop\u currency\u id`='2',shop\u specialprice`.`min\u quantity`0然后`shop\u specialprice`.`price`.`price`.`*(100-`shop\u折扣`.`percent`)`36.889479/100当`shop\u specialprice`.`min\u数量`0然后`price`.`100-`shop\u折扣`percent`.`36.889479/100其他`price`,如果(`shop\u items`.`shop\u currency\u id`='1',当`shop\u specialprice`.`min\u quantity`0然后`shop\u specialprice`.`price`.`price`*(100-`shop\u折扣`.`percent`)*1/100当`shop\u specialprice`.`min\u数量`0然后`price`.*.(100-`shop\u折扣`.`percent`.`price`)作为`price``
来自'shop_items'`
左外连接`商店\商品\折扣`
在'shop\u items'上。'id`='shop\u item\u折扣'。'shop\u item\u id`
左外连接'shop_specialprice`
在'shop\u items'上。'id`='shop\u specialprices`.'shop\u item\u id`
和“商店特价”。“最小数量”=“2014-02-14 19:27:18”
或“购物折扣”。“结束日期时间”=“0000-00-00:00:00”
还有“购物折扣”。“开始日期时间”2014-02-14 19:27:17
或'shop\u items'。'end\u datetime`='0000-00-00:00:00'
)
和(0,-1)中的'shop\u items'。'siteuser\u group\u id'
和'shop\u items'。'modification\u id`=0
和'shop_items'。'shop_group_id'位于('688','634','635','642','638','707','706','632')
和'shop_items'。'deleted`=0
有
`价格`>=5000
还有“价格”你忘了结局
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
SELECT SQL\u CALC\u FOUND\u ROWS`shop\u items`.*,如果(`shop\u items`.`shop\u currency\u id`=“3”,
当'shop\u specialprice`.'min\u quantity`为0时,则'shop\u specialprice`.'price`*(100-'shop\u折扣`.'percent`)*23.639664/100
当'shop\u specialprices`.'min\u quantity`0时,则'price`*(100-'shop\u折扣'.'percent`)*23.639664/100
否则"价格"结束,,
如果(`shop\u items`.`shop\u currency\u id`='2',`shop\u specialprices`.`min\u quantity`0,则`shop\u specialprices`.`price`*(100-`shop\u折扣`.`percent`)*36.889479/100
当'shop\u specialprices`.'min\u quantity`0时,则'price`*(100-'shop\u折扣'.'percent`)*36.889479/100
否则"价格"结束,,
如果(`shop\u items`.`shop\u currency\u id`='1',`shop\u specialprices`.`min\u quantity`0,则`shop\u specialprices`.`price`*(100-`shop\u折扣`.`percent`)*1/100
当'shop\u specialprices`.'min\u quantity`0时,则'price`*(100-'shop\u折扣'.'percent`)*1/100
或者'price'结束,price'))作为'price'
从'shop_items'开始
左外联接“shop\u items”上的“shop\u items\u折扣”。`id`=`shop\u items\u折扣`.`shop\u item\u id`
在'shop\u items'上左外连接'shop\u specialprices'。'id`='shop\u specialprices`。'shop\u item\u id`
和(`shop_specialprice`、`min_quantity`=`2014-02-14 19:27:18'或`shop_折扣`、`end_datetime`=`0000-00-00:00:00'和`shop_折扣`、`start_datetime`` 2014-02-14 19:27:17'或`shop_商品`、`end_datetime`=`0000-00-00:00:00')
和(0,-1)中的'shop\u items'。'siteuser\u group\u id'
和'shop\u items'。'modification\u id`=0
和'shop_items'。'shop_group_id'位于('688','634','635','642','638','707','706','632')
和'shop_items'。'deleted`=0
让'price`>=5000,'price`=5000,'price`尝试在和之间没有空格(
)。听说过代码格式吗?而且,你的问题也不清楚。你得到了什么错误?减去一,只是因为你在写一个好问题时没有付出努力。。
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