0

I try to get all rows which has a timestamp bigger then a value given in a variable or "0".

$pdo = $db->prepare('SELECT * FROM table ORDER BY id DESC, parent, cat WHERE timest >= :low OR timest = 0');
$pdo->bindParam(':low', $low, PDO::PARAM_INT);
$pdo->execute();

What is wrong with that?

I get a SQLState error 42000 (syntax error), but I don't understand why.

1
  • 1
    Your syntax is wrong, the order of the clauses is SELECT ... FROM ... WHERE ... ORDER BY. You swap ORDER BY and WHERE by adding some columns after ORDER BY. Please order the parts of your query. Commented Aug 10, 2014 at 0:01

1 Answer 1

1

The query is wrong:

SELECT * FROM table 
WHERE timest >= :low OR timest = 0
ORDER BY id DESC, parent, cat

The query base "format"/grammar is :

select [...]
[from ... [inner | left | right ] join ... [on ... | using ...]]
where ...
group by ...
having ...
order by ...
limit ...

Except for select, all parts are optional.

Better explanation in the mySQL Documentation here.

Sign up to request clarification or add additional context in comments.

1 Comment

even FROM is optional in MySQL, i.e. SELECT LAST_INSERT_ID(). There's no need for FROM dual as i.e. with Oracle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.