0

I have the following query which is returning an error. Can somebody please tell me what is wrong with my query? I looked at it again and again but everything seems fine to me.

Query

SELECT cities.latitude as lat, cities.longitude as long, cities.name as ctn, states.name as stn, countries.name as cn FROM cities
LEFT JOIN states ON cities.state_id = states.id
LEFT JOIN countries ON states.country_id = countries.id
WHERE cities.id = :id

Error

Warning: PDOStatement::execute(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'long, ct3.name as ctn FROM cities LEFT JOIN state...' at line 1 in
Dharman
  • 30,962
  • 25
  • 85
  • 135
Shubham Jha
  • 53
  • 1
  • 8

1 Answers1

1

Long is a MySQL Reserved word.

This means you need to encase it in backtick quotes to use it as a name, so the below should resolve your issue:

SELECT cities.latitude as lat, cities.longitude as `long`, cities.name as ctn, states.name as stn, countries.name as cn FROM cities
LEFT JOIN states ON cities.state_id = states.id
LEFT JOIN countries ON states.country_id = countries.id
WHERE cities.id = :id

Alternatively, rename your column identifier:

SELECT cities.latitude as citylat, cities.longitude as citylong, cities.name as ctn, states.name as stn, countries.name as cn FROM cities
LEFT JOIN states ON cities.state_id = states.id
LEFT JOIN countries ON states.country_id = countries.id
WHERE cities.id = :id
Martin
  • 22,212
  • 11
  • 70
  • 132