How can i get whole shop list in SQL

时间:2016-09-01 06:20:05

标签: mysql sql database

I would like to check each store's average score.

my ‘shop’ table is enter image description here

my ‘order’ table is enter image description here

My SQL is :

SELECT a.id,a.name,AVG(b.point) as point
from shop a
LEFT JOIN order b on a.id=b.shop_id
WHERE b.point<>0
GROUP BY(b.shop_id) 

but this SQL only can check out the shop had point in ‘order’ table.

How can i get whole shop list in SQL?

1 个答案:

答案 0 :(得分:3)

Please give this a try:

SELECT
    a.id,
    a.NAME,
    AVG(b.point) AS point
FROM
    shop a
LEFT JOIN `ORDER` b ON a.id = b.shop_id AND b.point <> 0
GROUP BY a.id

Since b.shop_id can be NULL so that you need to group by a.id in order to get result for all shop ids.