【发布时间】:2021-11-19 13:39:16
【问题描述】:
我收到此错误我想按每个月将每个月的总计与每个联系人组相加,但是当我运行查询时,它显示此错误 SQLSTATE[HY000]:一般错误:1111组函数使用无效(SQL:select GROUP_CONCAT(sum(documents.grand_total) SEPARATOR ",") as total
GROUP_CONCAT(documents.grand_total) as total 什么时候做我得到了所有的总数,但是这里分隔的每个联系人逗号的总和是我的 laravel 查询
$Months = DB::table('documents')
->join('contacts', 'documents.contact_id', '=', 'contacts.id')
->selectRaw('GROUP_CONCAT(sum(documents.grand_total) SEPARATOR ",") as total
,DATE_FORMAT(documents.due_date,"%M") as month ,
GROUP_CONCAT(DISTINCT contacts.name SEPARATOR ",") as names')
->where(
[
['documents.company_id', '=', session('current_company')],
['documents.document_type', '=', 1],
['documents.status', '=', 2],
]
)
->whereMonth('documents.created_at', '=', now())
->orWhereMonth('documents.created_at', '=', now()->subMonths(1))
->orWhereMonth('documents.created_at', '=', now()->subMonths(2))
->orWhereMonth('documents.created_at', '=', now()->subMonths(3))
->orWhereMonth('documents.created_at', '=', now()->subMonths(4))
->orWhereMonth('documents.created_at', '>', now())
->orWhereMonth('documents.created_at', '<', now()->subMonths(4))
->groupByRaw('DATE_FORMAT(documents.due_date,"%M")')
->orderBy('documents.due_date')
->get();
我现在得到的结果是所有documents.grand_total,只使用group_concat(documents.grand_total)是这样的
Illuminate\Support\Collection {#1570 ▼
#items: array:3 [▼
0 => {#1560 ▼
+"total": "53.51,53.51,53.51,53.51,53.51,53.51,53.51"
+"month": "March"
+"names": "Darrel Little"
}
1 => {#1565 ▼
+"total": "584.78,106.56,40.36,820.46,49.17,96.56,90.53,51.36,44.87,158.76,673.24,203.26,51.36"
+"month": "September"
+"names": "Dane Cruickshank IV,Darrel Little,Dr. Enola Marks,Electa Harris"
}
2 => {#1561 ▼
+"total": "53.51,108.58,108.58,108.58,353.35"
+"month": "October"
+"names": "Dane Cruickshank IV,Darrel Little,Dr. Wanda Hyatt MD"
}
]
}
但我希望每个联系人的总和以逗号分隔,请帮助我,我在过去两天卡在此表格上,提前谢谢。
【问题讨论】:
-
只用 SUM 替换
GROUP_CONCAT(SUM(...))? -
当我这样做时,它给出了上面提到的错误 1111 无效使用组函数(SQL:select group_concat(sum(documents.grand_total))
标签: mysql laravel group-by laravel-query-builder group-concat