Simpledev
Simpledev

Reputation: 647

Error with mysql request and reserved word as column name

In mysql table i have column named with reserved word "from", so i tried request with backtips like this

SELECT * FROM timesheets  WHERE user_id = 1 WHERE `from` <= 2013-10-31 WHERE `from` > 2013-01-01

but it doesn't work, there is an error :

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; 

Can you help me ?

Upvotes: 0

Views: 112

Answers (2)

ComFreek
ComFreek

Reputation: 29424

  • You have three WHERE clauses in your SQL statement.

    WHERE user_id = 1 WHERE `from` <= 2013-10-31 WHERE `from` > 2013-01-01
    
  • You didn't quote your dates.

These points make your SQL query invalid.

→ Replace your superfluos WHERE clauses by ANDs:

WHERE user_id = 1 AND (`from` <= '2013-10-31' AND `from` > '2013-01-01')

→ You can even use BETWEEN ... AND ... for Dates:

WHERE user_id = 1 AND (`from` BETWEEN '2013-01-01' AND '2013-10-31')

The parentheses should not be necessary, but I prefer adding them for readability.

Upvotes: 1

Filipe Silva
Filipe Silva

Reputation: 21657

Your query is invalid SQL. You can only have one WHERE clause in a query, and you have 3. As far as the "reserved" column name, you should be ok as long as you put the backticks (from).

You may be looking for something like this:

select *
from timesheets
where user_id = 1
AND `from` BETWEEN  '2013 - 01 - 01' AND '2013 - 10 - 31';

Upvotes: 0

Related Questions