duadpnld426905 2018-03-01 05:52
浏览 468
已采纳

SQL语句没有返回任何内容或在某些字段中返回NULL

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

  • 写回答

2条回答 默认 最新

  • dtwzwmv87399 2018-03-01 06:08
    关注
    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献