使用CASE和JOIN查询MySQL错误#1064

时间:2015-03-17 06:02:36

标签: mysql

执行以下语句会返回错误:

SELECT co.id,co.user_id,co.cat_id,co.offer_name,co.cost,u.shop_name, CASE WHEN buy >= get THEN buy
        else get  END as buy FROM createdoffers co INNER JOIN categories ct ON ct.id=co.cat_id INNER JOIN users u ON u.id=co.user_id 
        WHERE ct.Head_id='$head' AND ct.id=co.cat_id ORDER BY co.buy DESC LIMIT 1

MySQL返回:

#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 'get THEN buy
        else get  END as buy FROM createdoffers co INNER JOIN cate' at line 1 

我找不到错误,请解释此查询的错误。

1 个答案:

答案 0 :(得分:0)

get是一个关键字。用

逃避它
SELECT co.id,co.user_id,co.cat_id,co.offer_name,co.cost,u.shop_name, 
CASE WHEN buy >= `get` THEN buy else `get` END as buy 
FROM createdoffers co INNER JOIN categories ct ON ct.id=co.cat_id INNER JOIN users u ON u.id=co.user_id WHERE ct.Head_id='$head' AND ct.id=co.cat_id ORDER BY co.buy DESC LIMIT 1