2013-08-22 96 views
0

爲什麼我從這裏得到一個錯誤?什麼導致此MySQL查詢中的語法錯誤?

 

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 ; 

回答

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,你可以用雙引號代替,這是更便攜。)

+0

是啊。它做到了。謝謝! – ShoeLace1291

+0

+1直接鏈接到5.7手冊!先生,你領先於曲線。 –

+0

@ ShoeLace1291:不客氣,很高興幫助。 :-) –