lovephp Posted July 14, 2016 Share Posted July 14, 2016 im trying to display records where hot_topic ='Yes' comes on top as in DESC order but only if created timestamp is not older than 7 days else it should not show on top how would the query be here? $query = "SELECT * FROM posts ORDER BY hot_topic ='Yes' WHERE created !< DATE_SUB(NOW(), INTERVAL 7 DAY), id DESC LIMIT :per_page OFFSET :offset"; im sure this is definitely wrong Quote Link to comment https://forums.phpfreaks.com/topic/301481-result-order-by-with-following/ Share on other sites More sharing options...
Solution Psycho Posted July 14, 2016 Solution Share Posted July 14, 2016 (edited) It's not even valid syntax !< The WHERE clause is where you include/exclude data based on conditions. The ordering logic will go in the ORDER BY clause. Based on your request and the query I'm not sure if you are trying to exclude records based on the date or if you are wanting the date used for the ordering logic. I think this may be what you want SELECT * FROM posts -- First order by the CONDITION of hot_topic = 'yes' AND created in last 7 days -- Secondarily order by the created date ORDER BY (hot_topic ='Yes' AND created > DATE_SUB(NOW(), INTERVAL 7 DAY)) DESC, created DESC LIMIT :per_page OFFSET :offset Edited July 14, 2016 by Psycho 1 Quote Link to comment https://forums.phpfreaks.com/topic/301481-result-order-by-with-following/#findComment-1534513 Share on other sites More sharing options...
lovephp Posted July 14, 2016 Author Share Posted July 14, 2016 this was exactly what i wanted thank you soo much Quote Link to comment https://forums.phpfreaks.com/topic/301481-result-order-by-with-following/#findComment-1534514 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.