2012-08-03 118 views
2

我在我的PHP脚本中使用MySQL超过6年,但我从未遇到像这样的错误。MYSQL语法错误 - 为什么会发生这种情况?

当我执行SQL命令:

SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (show=1) LIMIT 1 

它抛出我这个错误

#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 'show=1) LIMIT 1' at line 1 

表结构是:

CREATE TABLE IF NOT EXISTS `discount_items` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `name` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL, 
    `image` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL, 
    `discount` float NOT NULL, 
    `price1` float NOT NULL, 
    `price2` float NOT NULL, 
    `bought` int(11) NOT NULL, 
    `target` int(11) NOT NULL, 
    `desc` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL, 
    `link` text CHARACTER SET utf8 COLLATE utf8_czech_ci NOT NULL, 
    `active` tinyint(1) NOT NULL, 
    `start` int(11) NOT NULL, 
    `end` int(11) NOT NULL, 
    `position` int(11) NOT NULL DEFAULT '1', 
    `show` int(11) NOT NULL DEFAULT '1', 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

我不明白什么是错。显然,“秀”场引起的问题,但我已经尝试了一切。(一定有什么毛病秀场,因为:

SELECT `discount_items`.* FROM `discount_items` WHERE (show=1) AND (active=1) AND (end<=1344007212) AND (position=1) LIMIT 1 

抛出

#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 'show=1) AND (active=1) AND (end&lt;=1344007212) AND (position=1) LIMIT 1' at line 1 

所以,问题与秀场移动。

我很抱歉,如果这是常见的问题,但我用Google搜索并没有发现这个错误太全局和没有解释任何东西给我。

感谢任何他lp和提示!

回答

3

show是保留字。请将其更改或将其放置在刻度中

SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (`show`=1) LIMIT 1 
+0

谢谢。我以为它会是这样的--_- :) – user1574556 2012-08-03 15:46:39

+0

我想,早起的鸟儿会发现蠕虫。我只是打出了这个问题的答案,同时发布了2个答案...... Upvoted他们两个...... :) – verisimilitude 2012-08-03 15:50:00

+0

我认为**结束**也是一个保留字 – hmmftg 2012-08-03 15:50:21

4

show是MySQL保留字。如果你想用它来引用一个字段名称,则必须将其反引号是这样的:

SELECT `discount_items`.* 
FROM `discount_items` 
WHERE 
    (position=1) 
    AND (active=1) 
    AND (end<=1344007212) 
    AND (`show`=1) /* backticks added here */ 
LIMIT 1 
1

show是一个MySQL的保留字。用反引号将它括起来使其起作用。

相关问题