Jump to content

result order by with following


Go to solution Solved by Psycho,

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/301481-result-order-by-with-following/
Share on other sites

  • Solution

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 by Psycho
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.