是什么导致MySQL查询中的语法错误?

时间:2013-08-22 23:17:49

标签: mysql select syntax

为什么我会收到错误?


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 'read, compose, reply, moderate FROM article_permissions' at line 2
SELECT 
permission_id, category_id, group_id, read, compose, reply, moderate
FROM article_permissions
WHERE category_id = 6
     AND group_id = 0 
ORDER BY permission_id DESC 
LIMIT 1

这是表格:

CREATE TABLE IF NOT EXISTS `article_permissions` (
  `permission_id` int(15) NOT NULL AUTO_INCREMENT,
  `category_id` int(15) NOT NULL,
  `group_id` int(15) NOT NULL,
  `read` tinyint(1) NOT NULL,
  `compose` tinyint(1) NOT NULL,
  `reply` tinyint(1) NOT NULL,
  `moderate` tinyint(1) NOT NULL,
  PRIMARY KEY (`permission_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

1 个答案:

答案 0 :(得分:3)

read是MySQL的reserved word。要在查询中将其用作列名,请将其放在反引号中(就像在表创建语句中一样)。

SELECT 
`permission_id`, `category_id`, `group_id`, `read`, `compose`, `reply`, `moderate`
FROM article_permissions
WHERE `category_id` = 6
     AND `group_id` = 0 
ORDER BY `permission_id` DESC 
LIMIT 1

(如果启用ANSI_QUOTES option,则可以使用双引号,这样更便携。)