News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

not smf related, how to make order by else order by

Started by shadav, August 11, 2021, 10:23:31 AM

Previous topic - Next topic

shadav

ok this is not smf related but I needs help (imagine that) and my normal go to guy isn't responding

how can I write an if statement or else statement for order by
WHERE collections_post.collection_id = "' . $Listing['id'] . '"
                GROUP BY posts.id
                ORDER BY collections_post.sortable ASC'
            )->all();


I need it to order by sortable else if null order by create year

ORDER BY collections_post.sortable ASC'
else
ORDER BY posts.create_year DESC'

shawnb61

Make $order_by a variable. If using $smcFunc, pass the value in the array.

The query is just a string & can be manipulated at will!   :)
Address the process rather than the outcome.  Then, the outcome becomes more likely.   - Fripp

shadav

#2
 :laugh: :laugh: :laugh: :laugh: was that even english
sorry but yeah ya lost me...I've no clue what I'm doing here and I tried to google it but came up empty

this at least doesn't give me any errors, but it also doesn't work

ORDER BY
CASE WHEN collections_post.sortable = !NULL THEN collections_post.sortable END ASC,
CASE WHEN collections_post.sortable = NULL THEN posts.create_year END DESC'


[edit]
I think I figured it out, seems to be working
ORDER BY
CASE WHEN collections_post.sortable IS NULL THEN posts.create_year
ELSE collections_post.sortable END ASC'


Thank you

SpacePhoenix

If you're using a GROUP BY clause, then you need to be aware of MySQL's "GROUP BY strict mode"

https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

Advertisement: