我需要选择所有产品:
select id, data, name from products where user= ?
过了一会儿,我打印了这个结果:
while{
product name, data
...
}
问题是,我需要计算每个产品的销量。 所以在这期间我有另一个选择,只是为了计数:
while{
select count(id) from sells where product_id = id
product name, data, sells count
...
}
所以我的问题很简单,我可以避免循环内的计数吗?我可以在第一个选择中只计数一次而不对结果进行分组吗?
最佳答案
select products.id, data, name, count(sells.id) as sell_count
from products
left join sells on sells.product_id=products.id
where user= ?
group by products.id
关于php - 循环内计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33128924/