SQLSTATE[HY000]: 一般错误: 1111 无效使用组函数 (SQL: select GROUP_CONCAT(sum(documents.grand_total) SEPARATOR &
Posted
技术标签:
【中文标题】SQLSTATE[HY000]: 一般错误: 1111 无效使用组函数 (SQL: select GROUP_CONCAT(sum(documents.grand_total) SEPARATOR ",") as total【英文标题】:SQLSTATE[HY000]: General error: 1111 Invalid use of group function (SQL: select GROUP_CONCAT(sum(documents.grand_total) SEPARATOR ",") as total 【发布时间】: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))
【参考方案1】:
GROUP_CONCAT(SUM()) 总是显示 #1111 - 无效使用组函数错误.. 因此,您可以使用此示例来学习一种方法,而不是添加总和,
SELECT customer_id,
GROUP_CONCAT(product_id,':',quantity_sum SEPARATOR ' ; ' )
FROM
(SELECT customer_id,product_id,sum(quantity) as quantity_sum
from plus2_sale group by customer_id,product_id) a
group by customer_id
所以在您的情况下,您的查询应该如下所示。我认为这会起作用
'select * , GROUP_CONCAT(sum_grand_total SEPARATOR ",") as total from (select * ,sum(grand_total) as sum_grand_total from document) as document_details ...'
您可以使用this tutorial 了解有关 group_concat 的更多信息
【讨论】:
以上是关于SQLSTATE[HY000]: 一般错误: 1111 无效使用组函数 (SQL: select GROUP_CONCAT(sum(documents.grand_total) SEPARATOR &的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE[HY000]:一般错误:1364 字段“parent_id”没有默认值
SQLSTATE [HY000]:一般错误:1005无法创建表`Projectname`.`users`(errno:150“外键约束格式不正确”)[重复]