想使用左连接和计数
want to use left join and count
请求如下:
select offers.*,
count(*)
from offers left join products on (products.offerId=offers.id)
group by offers.id order by offers.id desc;
我想获取所有offer,包括里面没有商品的offer。
这部分工作正常。
我的问题是请求的 count(*)
部分。当报价中没有产品时,我预计 0
。但是我得到 1
。我理解这可能是行本身(在 offers
table 中)。我试过 count(products.*) 但出现 mysql 错误...如何继续?
斯坦
进行嵌套select
Select , (select count() from products p where p.offerid = o.id) as countofoffers
来自报价 o
根据 Joachim Isaksson 说:
我需要计算 "right" table 中的单个字段,products.offerId
似乎很合适。
请求如下:
select offers.*,
count(*)
from offers left join products on (products.offerId=offers.id)
group by offers.id order by offers.id desc;
我想获取所有offer,包括里面没有商品的offer。 这部分工作正常。
我的问题是请求的 count(*)
部分。当报价中没有产品时,我预计 0
。但是我得到 1
。我理解这可能是行本身(在 offers
table 中)。我试过 count(products.*) 但出现 mysql 错误...如何继续?
斯坦
进行嵌套select
Select , (select count() from products p where p.offerid = o.id) as countofoffers 来自报价 o
根据 Joachim Isaksson 说:
我需要计算 "right" table 中的单个字段,products.offerId
似乎很合适。