douzhi9635 2014-10-03 05:16
浏览 19
已采纳

mysql join返回0表示一列,其他列完好无损

I wish to join multiple tables like- Categories, menus, restaurants, reviews, etc. to return the restaurants that provide the inserted food with their prices. Everything works except numberOfReviews in reviews table.

If a restaurant has no reviews then output should be 0 for numOfReviews column but other column values should be retrieved i.e. price, name, etc.

With following query I get all fields as null and count(numReviews) as 0:

 select r.id
     ,r.`Name`
     ,r.`Address`
     ,r.city
     ,r.`Rating`
     ,r.`Latitude`
     ,a.`AreaName`
     ,m.`Price`
     ,count(rv.id)
 from `categories` c, `menus` m, `restaurants` r, areas a, reviews rv
 where m.`ItemName`="tiramisu"
     and c.`restaurant_id`=r.`id`
     and m.`category_id`=c.id
     and r.`AreaId`=a.`AreaId`

and if I can't match rv.restaurant_id=r.id in where clause(obviously).

Where am I getting wrong? How do I solve this?

edited

    select  r.id, 
 r.`Name`,
 r.`Address`,
 r.city,
 r.`Rating`,
 r.`Latitude`,
 a.`AreaName`, 
 m.`Price`, 
 r.`Longitude`, 
 r.Veg_NonVeg,
 count(rv.id)
 from restaurants r LEFT JOIN `reviews` rv on rv.`restaurant_id`=r.`id`
inner join `categories` c on c.`restaurant_id` = r.id
inner join `menus` m on m.`category_id` = c.id
inner join `areas` a on a.`AreaId` = r.`AreaId`
 where m.`ItemName`="tiramisu"
  • 写回答

1条回答 默认 最新

  • duanni5726 2014-10-03 05:34
    关注

    First of all, don't use this old school syntax for the jointures.

    Here is a query that may solve your problem:

    SELECT R.id
        ,R.Name
        ,R.Address
        ,R.city
        ,R.Rating
        ,R.Latitude
        ,R.Longitude
        ,A.AreaName
        ,M.Price
        ,R.Veg_NonVeg
        ,COUNT(RV.id) AS numOfReviews
    FROM restaurants R
    INNER JOIN categories C ON C.restaurant_id = R.id
    INNER JOIN menus M ON M.category_id = C.id
    INNER JOIN areas A ON A.AreaId = R.AreaId
    LEFT JOIN reviews RV ON RV.restaurant_id = R.id
    WHERE M.ItemName = 'tiramisu'
    GROUP BY R.id, R.Name, R.Address, R.city, R.Rating, R.Latitude, R.Longitude, A.AreaName, M.Price, R.Veg_NonVeg
    

    I used explicit INNER JOIN syntax instead of your old school syntax and I modified the jointure with table reviews in order to get the expected result. The GROUP BY clause is required to use the aggregate function COUNT, every rows will be grouped by the enumerated columns (every column except the one used by the function).

    Here is another solution that simplify the GROUP BY clause and allow the modification of SELECT statement without having to worry about the fact that every columns need to be part of the GROUP BY clause:

    SELECT R.id
        ,R.Name
        ,R.Address
        ,R.city
        ,R.Rating
        ,R.Latitude
        ,R.Longitude
        ,A.AreaName
        ,M.Price
        ,R.Veg_NonVeg
        ,NR.numOfReviews
    FROM restaurants R
    INNER JOIN (SELECT R2.id
                    ,COUNT(RV.id) AS numOfReviews
                FROM restaurants R2
                LEFT OUTER JOIN reviews RV ON RV.restaurant_id = R2.id
                GROUP BY R2.id) NR ON NR.id = R.id
    INNER JOIN categories C ON C.restaurant_id = R.id
    INNER JOIN menus M ON M.category_id = C.id
    INNER JOIN areas A ON A.AreaId = R.AreaId
    WHERE M.ItemName = 'tiramisu'
    

    As you can see here I added a new jointure on a simple subquery that does the aggregation job in order to provide me the expected number of reviews for each restaurant.

    Hope this will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)