I’ve got a table category that links category numbers to category names.
e.g., 1-Fruit, 2-Vegetables, 3-Insects, 4-Cities etc…
I’ve got another table item that has items that belong to exactly one category. Each item has a unique id, the name and its category. e.g., 314-Pumpkin-(category)1
In short:
category
-------
c_id (int)
c_name (varchar)
item
-------
i_id (int)
i_name (varchar)
i_cat (int)
The problem is that not all categories have items. I’ve used the following to get items ordered by category:
$STH = $DBH->prepare("SELECT * FROM item ORDER BY i_cat");
This is built into a table, extracting headings as the cat(egory) changes. Categories without items don’t get picked up, of course.
What I want is a table which includes all categories (even those without actual items) and that lists the correct items underneath each category.
I can devise some cumbersome method to do what I want, but there must be a neat solution. How can I join the data from both tables in one call so that every category and every item is present? I presume it involves the use of inner join, but I can’t see how to do it efficiently.
Thanks.
try the following query, it will get all the categories from the
categorytable and corresponding items from theitemtable: