Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - Column Name already named 'TimeStamp', need to use it in Where Clause

Tags:

sql

mysql

linqpad

What's the proper way to write this query? I have a column named TimeStamp in my customers table. I'm getting errors when trying to find customers who created an account in 2012. I've tried:

SELECT 'TimeStamp' AS CreatedDate
FROM customers
WHERE 'CreatedDate' >= '2012-01-01' AND 'CreatedDate' <= '2012-12-31'

and also

SELECT *
FROM customers
WHERE 'TimeStamp' >= '2012-01-01' AND 'TimeStamp' <= '2012-12-31'

and always get no results (there should be thousands)

like image 581
Ken Avatar asked Mar 23 '23 08:03

Ken


1 Answers

You must not use single quotes around column names as they are identifiers.

SELECT *
FROM customers
WHERE TimeStamp >= '2012-01-01' AND TimeStamp <= '2012-12-31'

If it happens that your column name is a reserved keyword, you can escape it by using backtick eg,

WHERE `select` ....    -- SELECT is a reserved keyword

or use it along with the tableName

FROM tb
WHERE tb.select ....   -- SELECT is a reserved keyword
like image 67
John Woo Avatar answered Mar 25 '23 04:03

John Woo