Combine data from two different join tables

Part of my circuit is depicted as Category, Items

I have two jump tables, one that displays the items in a category and the other that displays which items are "featured" for a particular category.

Now I need to get all the items belonging to a specific category (not difficult), but I also need to have a "featured" column that will show up if an item is enabled for that category (NULL if not featured).

I have tried various combinations of LEFT JOIN like

SELECT i.*, category.name, category_feat_item.item_id AS featured
FROM item AS i
INNER JOIN item_category ON i.id = item_category.item_id
INNER JOIN category ON category.id = item_category.category_id AND category.id =1
LEFT OUTER JOIN category_feat_item ON i.id = category_feat_item.item_id
ORDER BY featured DESC

      

but I'm stumped.

+1


source to share


1 answer


Looks almost ok, but you also need to assign the category_id in the left outer join. Otherwise, you will receive all recognized items of the item:



SELECT i.*, category.name, category_feat_item.item_id AS featured
FROM item AS i
INNER JOIN item_category ON i.id = item_category.item_id
INNER JOIN category ON category.id = item_category.category_id AND category.id =1
LEFT OUTER JOIN category_feat_item ON i.id = category_feat_item.item_id AND category_feat_item.category_id = 1
ORDER BY featured DESC

      

+1


source







All Articles