0

I can't figure out what is wrong with the following query:

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;

It give me the following error when run with python's MySQLdb:

_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")

I am new to SQL, and your help is much appreciated.

1 Answer 1

1

The problem line is

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

continue is a reserved word and you need to backtick something as

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

Here what happens in mysql when your query comes to this line

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

Now if I use backtick on my test table I get

mysql> select IF(uname REGEXP '[;}[:space:]]continue;', 'true', 'false')
 AS `continue` from test ;
+----------+
| continue |
+----------+
| false    |
| false    |
| false    |
| false    |
| false    |
| false    |
| false    |
+----------+
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I should have known to look at reserved words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.