2

我在我的 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;

我不明白有什么问题。显然,“显示”字段会导致问题,但我已经尝试了一切......(显示字段一定有问题,因为:

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

所以问题转移到了显示领域。

如果这是常见问题,我很抱歉,但我用谷歌搜索却一无所获。这个错误太全球化了,对我没有任何解释。

感谢您的帮助和提示!

4

3 回答 3

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
于 2012-08-03T15:46:43.620 回答
3

show是保留字。要么改变它,要么把它放在蜱虫中

 SELECT `discount_items`.* FROM `discount_items` WHERE (position=1) AND (active=1) AND (end<=1344007212) AND (`show`=1) LIMIT 1
于 2012-08-03T15:46:11.637 回答
1

show是 MySQL 的保留字。将其括在反引号中以使其工作。

于 2012-08-03T15:47:26.850 回答