Search code examples
phpmysqlsqlleft-joininner-join

SQL statement not returning anything or returning NULL in some fields


I've looked at quite a few questions on here and other sites and still cannot see why my SQL statement is returning nothing.

Basically I've got a database called 'takeaway' split into two tables categories and menuitems.

I want to output each category name catname and the menu items associated with each category for example Cod Fillet is part of category 2 'catid'.

Categories Table Fields:

catid
catname

Menuitems Table Fields:

itemid
itemname
itemsize
itemprice
categoryid (foreign key)

I've used the following SQL statement but is producing nothing

select
    menuitems.itemname,
    menuitems.size,
    menuitems.price,
    menuitems.categoryid,
    categories.catid,
    categories.catname 
FROM categories 
RIGHT JOIN menuitems
    ON categories.catid=menuitems.categoryid

I've tried an INNER JOIN and LEFT JOIN. Nothing has worked. I want to output like so:

Fish
    - Cod Fillet Large: £4.90 Medium: £3.90

And so on... obviously I'm doing this using Mysql and PHP.

Please can someone help point out where I'm going wrong?

Thanks

Dan


Solution

  • select
        menuitems.itemname,
        menuitems.itemsize,
        menuitems.itemprice,
        menuitems.categoryid,
        categories.catid,
        categories.catname
    FROM categories AS  categories
    Left JOIN menuitems AS menuitems
        ON categories.catid = menuitems.categoryid;