I have the following tables:
Collections ( 'id', 'name') (1, 'one collection') ItemToCollection ( 'item_id', 'collection_id') (1,1), (2,1), (3,1), ...etc Items ( 'id', 'name' ) (1, 'one'), (2, 'two'), (3, 'three'), (4, 'four'), (5, 'five'), (6, 'six'), (7, 'seven') CollectionsPositions ( 'col_id', 'item_id', 'pos' ) (1, 2, 2), (1, 4, 7), (1, 1, 4)
I want to return items ordered in the order specified in CollectionsPositions for Collection 1, but the problem is that I donβt have positions for all elements, and I canβt organize ORDER BY, having NULL values ββfor some elements.
I tried something like this:
SELECT FROM ItemToCollection JOIN Item ON ItemToCollection.item_id = Item.id LEFT JOIN CollectionsPositions ON col_id = 1 AND item_id = ItemToCollection.item_id WHERE ItemToCollection.collection_id = 1
This will return me something like this: column 'pos':
NULL, NULL, NULL, NULL, NULL, 2, 4, 7
How can I order the items in the following order :?
NULL, 2, NULL, 4, NULL, NULL, 7
I understand that I need to make a new column and sort by this column, but how do I handle already certain positions when I create a new column for an order?