英文:
groupby sum and count tickets
问题
在我的练习中,我必须统计销售的票数并累加总收入。
$tickets = Ingresostaquilla::groupBy('tipo')
->selectRaw('count(*) as quantity, tipo')
->get();
我已经按票种进行了计数,但我不知道如何累加每种票的总金额。
模型表格:
我应该使用 SUM
,但如何在 groupBy
中实现呢?
英文:
I am learning to use groupBy
.
In my exercise, I must count the number of tickets that were sold and add the total money collected.
$tickets = Ingresostaquilla::groupBy('tipo')
->selectRaw('count(*) as quantity, tipo')
->get();
I did the count by type of ticket, but I don't know how to make it add up to the total amount of money that each ticket collects.
Model table:
I should use SUM
, but how do I do it with groupBy
?
答案1
得分: 1
您可以尝试这个查询,它将对每种类型的门票筹集到的金额进行求和。
英文:
$tickets = Ingresostaquilla
->selectRaw('tipo, sum(cantidad) as quantity, sum(total)')
->groupBy('tipo')
->get();
You can try this query, it will sum the amount of money raised for each type of ticket.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论