Order by selected value in form

I’m building (yet another pointless) web sorting tool that had two criteria (category and topic) that appear on a web form as checkboxes. The user picks their categories, picks their topics, hits submit, and gets a list of records that match. The topic list is long, so to keep the list from taking up a huge amount of space, I’ve put those checkboxes in a div that scrolls.

I build the list of topics using a query, and I order them by the topic name to make them alphabetical. Once the user submits the form, I’d like the selected topics to appear at the top of the list, followed by the rest of them. I managed to do this so far with two queries, one that gets the selected topics, and another that gets the unselected ones, but I’m wondering if there’s a way to do it with one query, and somehow pass the list of selected topic IDs to the ORDER BY.

ORDER BY CASE WHEN topic_id IN ( 9, 37 ) -- list of selected topic ids THEN 'dumpty' ELSE 'humpty' END , topic_name

That works!!! :smiley:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.