0
SELECT * FROM answers, user WHERE answers.user = user.user

Hello everyone!

I have two MySQL-tables answers and user. The table user has a variable called user.

Unfortunately, user seems to be a keyword in MySQL. Can anyone please tell me where I have to put quotation marks so that it works? Unfortunately I couldnt get it running yet. Thanks!

D. Studer
  • 1,711
  • 1
  • 16
  • 35
  • 1
    You don't user is a keyword..if it was a reserved word then backticks would be required so what's your real problem?, whats your error message? AND use proper joins.. – P.Salmon Sep 12 '20 at 12:23
  • You put the backticks around the reserved word or keyword. Better yet, you don't use reserved words or keywords as column names. What is your actual question? – Ken White Sep 12 '20 at 12:26
  • 'The table user has a variable called user' - the table answers does not? – P.Salmon Sep 12 '20 at 12:26
  • The table user has a **column** named user, not a **variable**. If the answer table doesn't have a column named user, then clearly you can't use that as your JOIN condition. If the column isn't there, then no amount of backticks is going to help. I'd suggest you find a good basic SQL book or tutorial. – Ken White Sep 12 '20 at 12:28
  • Does this answer your question? [Syntax error due to using a reserved word as a table or column name in MySQL](https://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Soumendra Mishra Sep 12 '20 at 13:21

1 Answers1

3

In MySql put back ticks around each occurrence of the table or column name that's also a reserved word.

SELECT *
  FROM answers, `user` 
 WHERE answers.`user` = `user`.`user`

Better yet, don't use reserved words for database, table, or column names. Remembering the backticks is a pain in the **s xxx neck, and you get strange error messages when you forget them.

Also, come on into the 21st century. Drop the old-timey comma join syntax. Say this instead.

SELECT *
  FROM answers
  JOIN `user` ON answers.`user` = `user`.`user`
O. Jones
  • 103,626
  • 17
  • 118
  • 172