4
SELECT m.id, m.title, m.message, m.from, m.to2, m.to_viewed, m.from_viewed, m.created, u.username 
FROM tbl_messages m 
INNER JOIN tbl_users u 
ON m.from = u.id WHERE m.to2 = '1' && m.to_saved = '1'  && m.to_deleted = '0' 
ORDER BY m.created DESC

Having removed m.from, the query runs. It doesn't like this field name. Is 'from' reserved? Could someone suggest a fix?

Thanks

cillierscharl
  • 7,043
  • 3
  • 29
  • 47
sark9012
  • 5,485
  • 18
  • 61
  • 99
  • You should use backticks [`] with the keywords that are used as a column name or table name. – Ghazanfar Mir Sep 19 '11 at 11:54
  • possible duplicate of [Syntax error due to using a reserved word as a table or column name in MySQL](http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql) – Ian Ringrose May 06 '14 at 09:56

5 Answers5

3

Wrap it with backticks, because it is mysql's reserved keyword

m.`from`

Here is a full list of reserved words

zerkms
  • 249,484
  • 69
  • 436
  • 539
3

Yes, it's one of the reserved words. Use backticks to quote it:

m.`from`
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

From is reserved, yes. You could try adding quotes around it. The easiest is to avoid using reserved words in queries.

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
1

Yes it is a reserved word. You should enclose from in back ticks like

m.`from`
Jan S
  • 1,831
  • 15
  • 21
0

Yes, "from" is reserved. In mysql, IIRC you can put it in either double quotes (") or backquotes (`) (but I also use postgresql, so I may be confusing the two systems).

Colin Fine
  • 3,334
  • 1
  • 20
  • 32