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 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;以上代码出现了You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'grant , ( SELECT COUNT(1) FROM user_coupon WHERE activity_id = a.id AND (status ' at line 1这个错误,修复他
时间: 2024-04-12 22:31:13 浏览: 169
这个错误是因为在查询中使用了保留字 `grant` 作为列名,而 `grant` 是 MySQL 中的一个保留字,不能直接作为列名使用。为了修复这个错误,你需要修改列名 `grant` 为其他合法的名称。
以下是修复后的代码示例:
```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;
```
在上面的修复中,将 `grant` 列名修改为 `total_grant`。你可以根据需要选择其他合适的列名来替换原来的 `grant` 列名。修复后的代码应该可以正确执行了。
阅读全文
相关推荐


















