Possible Duplicate:
Can you add “IF” statement in PHP MYSQL ORDER BY?
How can I use IF condition for ORDER in MySQL?
For instance, my query below that returns an error,
SELECT *
FROM page AS p
WHERE p.parent_id != p.page_id
AND p.type = 'post'
AND p.parent_id = '7'
IF(
'date created' = 'date created',
ORDER BY p.created_on DESC,
ORDER BY p.created_on ASC
)
message,
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IF( 'date created' = 'date created', ORDER BY p.created_on DESC, ORDER BY p.' at line 17
The first 'date created' is variable. So if 'date created' = 'date created'
,
then ORDER BY p.created_on DESC
else ORDER BY p.created_on ASC