Craig
Craig

Reputation: 195

Querying a table that has column names with spaces?

I am currently trying to get table data that have column names with spaces.

The client has created the database with column names having spaces, the table is joined across other tables, I have tried using the column name in [] and '' but don't seem to work. My query is as follows:

$query = "SELECT appliances.*, location.* FROM (appliances LEFT JOIN location ON appliances.Location ID = location.ID) WHERE Location ID = 11";

Upvotes: 1

Views: 2546

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74216

"I asked the client to change the column to have an underscore but this column is linked across many tables in there database."

Since the column name cannot be changed as outlined in comments, give this a try.

appliances.`Location ID` = location.ID) WHERE `Location ID`

or

`appliances`.`Location ID` = location.ID) WHERE `Location ID`
  • Notice those are ticks and not regular single quotes.

Check for errors also. I do not know which API is being used to connect with, nor on which platform.

  • This (example) answer is based on MySQL and not MSSQL, since the question was tagged as mysql.

Upvotes: 2

Related Questions