MYSQL语法错误 - 为什么会发生这种情况?

时间:2012-08-03 15:44:10

标签: mysql sql syntax

我在我的php脚本中使用mysql超过6年但我从未遇到过这样的错误。

当我执行此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;

我不知道什么是错的。显然'show'字段导致了问题,但我已经尝试了所有的东西..(显示字段一定有问题因为:

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

所以问题随着show field而移动。

我很抱歉,如果这是常见的问题,但我用谷歌搜索,什么也没找到。这个错误太全局了,对我没有任何解释。

感谢您的帮助和提示!

3 个答案:

答案 0 :(得分: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 :(得分:3)

show是保留字。要么改变它,要么把它放在刻度线

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

答案 2 :(得分:1)

show是MySQL保留字。将它包含在反引号中以使其有效。