Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 找出有多少次‘;柴’;产品已经订购了吗?_Mysql_Sql - Fatal编程技术网

Mysql 找出有多少次‘;柴’;产品已经订购了吗?

Mysql 找出有多少次‘;柴’;产品已经订购了吗?,mysql,sql,Mysql,Sql,我是stackoverflow的新手,最近我作为开发人员接受了一次面试,有人问了我一个关于SQL的问题 给定两个表product and orders,如何查找已订购的prodcut'Chai'的名称您可以在公共列上使用join,如下所示 select name, coalesce(count(o.*), 0) as total from product p join orders o on p.product_id = o.product_id where name = 'Chai'

我是stackoverflow的新手,最近我作为开发人员接受了一次面试,有人问了我一个关于SQL的问题


给定两个表product and orders,如何查找已订购的prodcut'Chai'的名称

您可以在公共列上使用
join
,如下所示

select
  name,
  coalesce(count(o.*), 0) as total
from product p
join orders o
on p.product_id = o.product_id
where name = 'Chai'
group by
  name

请添加示例数据和所需输出。如果有人订购2个柴,订单表中会是一行还是两行柴?也许面试官应该澄清,如果从未订购过柴,会发生什么情况。