Rpant
Rpant

Reputation: 1054

Simple WHERE clause for column name 'User ID'?

I am not able to run a simple select query with a where clause. I am using MySQL and have a column User ID.

The problem is with the column name made of two words.

select * from user where 'User ID' = "xyz"

A usual query like the next one runs fine as expected:

select * from user where email = 'xyz'

How can I write a condition on the User ID column?

Upvotes: 0

Views: 755

Answers (4)

Lincoln Pires
Lincoln Pires

Reputation: 348

Like Jonathan Leffler pointed, if you are in MS SQL Server you can try:

select * from user where [User ID] = "xyz"

or in MySql PHP Admin:

select * from user where ´User ID´ = "xyz"

I am not sure about MySql, but in SQL Server, even "login" or "user" that are some reserved words, they are functional in queries normally. Despite, I think is better not to use that.

I hope this can help you or another one. Hugs.

Ref.:
1. Meaning of square brackets [] in MS-SQL table designer?
2. When to use single quotes, double quotes, and backticks in MySQL

Upvotes: 1

Maaaaat
Maaaaat

Reputation: 336

No way to rename this column ?
You can try with backticks around the column name in your query :

select * from user where `User ID` = 'xyz';

Upvotes: 1

Ian Wood
Ian Wood

Reputation: 6573

I think user is a reserved word...

Try:

SELECT *
FROM `user`
WHERE `email` = 'xyz';

Upvotes: -1

mechanical_meat
mechanical_meat

Reputation: 169284

Try:

SELECT u.* 
FROM   `user` u
WHERE  u.`User ID` = 'xyz';

But in general, try not to use such column names.

Using backticks to qualify the table and/or column names is also useful if you have names that conflict with MySQL keywords, e.g. user.

Upvotes: 2

Related Questions