SELECT c.stock_id, a.stock_name, a.coupon_amount, a.transaction_minimum, b.company_name, a.start_time, a.end_time, (SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id) AS total_grant, (SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id AND (status = "20" OR status = "30")) AS receive, (SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id AND status = "30") AS writeOff FROM wx_coupon a LEFT JOIN baishiof_company b ON a.company_id = b.company_id LEFT JOIN user_coupon c ON a.id = c.activity_id GROUP BY c.stock_id, a.stock_name, a.coupon_amount, a.transaction_minimum, b.company_name, a.start_time, a.end_time;以上代码存在这个问题Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'baishiof.a.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by修复他
时间: 2024-04-12 17:31:15 浏览: 195
PCB_设计技巧百问.pdf
要修复这个错误,你可以使用子查询来获取每个活动的 id,并将其作为一个聚合列进行处理。这样可以解决 "Expression #8 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'baishiof.a.id'" 这个错误。以下是修复后的代码示例:
```sql
SELECT
c.stock_id,
a.stock_name,
a.coupon_amount,
a.transaction_minimum,
b.company_name,
a.start_time,
a.end_time,
(SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id) AS total_grant,
(SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id AND (status = "20" OR status = "30")) AS receive,
(SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id AND status = "30") AS writeOff
FROM
wx_coupon a
LEFT JOIN
baishiof_company b ON a.company_id = b.company_id
LEFT JOIN
user_coupon c ON a.id = c.activity_id
GROUP BY
c.stock_id, a.stock_name, a.coupon_amount, a.transaction_minimum, b.company_name, a.start_time, a.end_time, a.id;
```
在上面的修复中,将 `a.id` 添加到 GROUP BY 子句中。这样可以确保每个活动的 id 在查询结果中是唯一的,并解决 "Expression #8" 的错误。
请注意,根据你的实际需求和数据库结构,你可能需要调整 GROUP BY 子句中的其他列。修复后的代码应该可以正确执行了。
阅读全文