php - how to conditionally order by two different categories from two different tables -


i have 3 tables joined together. 2 of tables ("posts" , "shared_posts") have date categories. want order date, if shared_posts.username = $username want use shared_posts.date instead of posts.date. (and vice-versa)

i'm not sure how that. here's have far...

select postid, posts.date, shared_posts.date, posts.username, posts.title posts left join posts_views_likes using (postid)  left join shared_posts using (postid)  posts.username = '" . $username . "' or shared_posts.username = '" . $username . "' order posts.date desc, shared_posts.date desc 

what efficient way this?

you can use case in order by clause:

order   case when shared_posts.username = '" . $username ."'     shared_posts.date     else posts.date   end desc 

Comments