mybatis 用字符串链接,报错SQL: SELECT a.*,b.ShopType FROM wsy_dream.ShopDistribute a INNER JOIN ( ? ) b INNER JOIN wsy_o2o.weixin_cityarea_supply c ON a.Activity_id = b.ID WHERE a.Isvalid=1 and a.Acquirer_id = ? and a.Shop_id = c.id and c.types = 20 ORDER BY CASE a.Consumption WHEN 1 THEN 99 END DESC, a.ID DESC; ### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ''SELECT * FROM wsy_dream.ShopActivity WHERE FIND_IN_SET(2, ShopType) > 0 UNION S' at line 4
时间: 2024-03-17 09:43:44 浏览: 143
这个错误是因为在拼接 SQL 语句时,拼接的字符串中含有 SQL 语法错误,导致整个 SQL 语句执行失败。
具体来说,错误在于第 4 行的子查询语句中,拼接的字符串中存在问题。根据错误信息中的提示,拼接的字符串中缺少了一对括号。应该将这个子查询语句改为:
```
SELECT * FROM wsy_dream.ShopActivity WHERE FIND_IN_SET(2, ShopType) > 0
UNION
SELECT * FROM wsy_dream.ShopActivity WHERE FIND_IN_SET(4, ShopType) > 0
```
即在两个子查询语句之间加上括号,使其成为一个整体。修改后的 SQL 语句应该是:
```
SELECT a.*,b.ShopType FROM wsy_dream.ShopDistribute a
INNER JOIN (
(SELECT * FROM wsy_dream.ShopActivity WHERE FIND_IN_SET(2, ShopType) > 0
UNION
SELECT * FROM wsy_dream.ShopActivity WHERE FIND_IN_SET(4, ShopType) > 0)
) b
INNER JOIN wsy_o2o.weixin_cityarea_supply c ON a.Activity_id = b.ID
WHERE a.Isvalid=1 AND a.Acquirer_id = ? AND a.Shop_id = c.id AND c.types = 20
ORDER BY CASE a.Consumption WHEN 1 THEN 99 END DESC, a.ID DESC;
```
注意括号的位置和数量,确保 SQL 语法正确。
阅读全文