2014-06-25 29 views
0
語句

我無法弄清楚什麼是錯用下面的查詢:錯誤在MySQL查詢有關IF在SELECT

_mysql_exceptions.ProgrammingError: (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 
'continue,\n\t\t\t\t\t\t\tIF(t_sh.content REGEXP '[;}[:space:]]throw ', 'true', 'false') ' at line 6") 
:當用python的MySQLdb的運行

SELECT t_sh.source_file_id, 
     t_sh.id, 
     MAX(t_sh.master_event_id), 
     IF(t_sh.content REGEXP '[;}[:space:]]break;', 'true', 'false') AS break, 
     IF(t_sh.content REGEXP '[;}[:space:]]break ', 'true', 'false') AS break_label, 
     IF(t_sh.content REGEXP '[;}[:space:]]continue;', 'true', 'false') AS continue, 
     IF(t_sh.content REGEXP '[;}[:space:]]throw ', 'true', 'false') AS throw, 
     IF(t_sh.content REGEXP '[;}[:space:]]return;', 'true', 'false') AS void_return 
FROM  source_histories AS t_sh, 
     ( SELECT DISTINCT source_file_id 
      FROM source_histories 
      WHERE content <> NULL 
       AND content REGEXP '[;}[:space:]]break;|[;}[:space:]]break |[;}[:space:]]continue;|[;}[:space:]]throw |[;}[:space:]]return;' 
     ) AS t_uniqueSFI 
WHERE t_sh.source_file_id = t_uniqueSFI.source_file_id; 

它給我下面的錯誤

我是SQL新手,非常感謝您的幫助。

回答

1

問題行是

IF(t_sh.content REGEXP '[;}[:space:]]continue;', 'true', 'false') as continue 

繼續是一個reserved word,你需要反引號的東西作爲

IF(t_sh.content REGEXP '[;}[:space:]]continue;', 'true', 'false') as `continue` 

這裏在mysql中會發生什麼,當你查詢涉及到這條線

mysql> select IF(uname REGEXP '[;}[:space:]]continue;', 'true', 'false') 
AS continue from test ; 
ERROR 1064 (42000): 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 'continue from test' at line 1 

現在,如果我在測試桌上使用反色,我會得到

mysql> select IF(uname REGEXP '[;}[:space:]]continue;', 'true', 'false') 
AS `continue` from test ; 
+----------+ 
| continue | 
+----------+ 
| false | 
| false | 
| false | 
| false | 
| false | 
| false | 
| false | 
+----------+ 
+0

非常感謝!我應該知道看保留字。 – sdsmith