I have a simple SQL statement that has "ORDER by title". Results look like this (title, desc):
Bobs Bakery A bakery Jims Shop Tims Carshop For car needs Wonkers Funhouse Virginia Town A great place
I want to change it so it sorts by title, and anywhere there is no desc (NULL) those items are at the bottom of the list. So the results look like this:
Bobs Bakery A bakery Tims Carshop For car needs Virginia Town A great place Jims Shop Wonkers Funhouse
I know I can do this in 2 SQL select statements, but I'm looking on how to do it in one.
$query = "SELECT DISTINCT relation.itemid, title, owner, image, background, icon, data_txt, IF(data_txt IS NULL or data_txt='',1,0) AS isnull FROM `#__sobi2_fields_data` AS fielddata, `#__sobi2_cat_items_relations` AS relation " . "LEFT JOIN `#__sobi2_item` AS items ON relation.itemid = items.itemid " . "WHERE ((`catid` = {$catid} {$or}) AND `published` = '1' AND (`publish_down` > '{$now}' OR `publish_down` = '{$config->nullDate}' ) AND items.itemid IN({$query}) ) AND items.itemid = fielddata.itemid AND fielddata.fieldid = 13 ORDER BY isnull ASC, {$config->listingOrdering} {$limits}";
foo=> select * from blah order by case when description is null then 1 else 0 end, title; title | description ——————+————— Bobs Bakery | A bakery Tims carshop | For car needs Virginia Town | A great place Jims shop | Wonkers funhouse | (5 rows)
I want to change it so it sorts by title, and anywhere there is no desc (NULL) those items are at the bottom of the list. So the results look like this:
I know I can do this in 2 SQL select statements, but I'm looking on how to do it in one.