mysql 查询时指定校对规则

时间:2021-01-24 06:55:00

为了能在查询时忽略字段大小写,又不想修改数据表的属性配置,就在SQL语句中做了修改。结果在alibaba druid 执行时报错

com.alibaba.druid.sql.parser.ParserException: syntax error, error in :'ame  collate 'utf8mb4_general_ci' f', expect RPAREN, actual LITERAL_CHARS pos 166, line 1, column 147, token LITERAL_CHARS utf8mb4_general_ci

这个错误在sql的命令行执行没有问题,但是druid 会解析失败,原因是collate用错了地方,只能用在where的查询条件里。

select app.id,app.token,app.email,app.retry,app.interval_time,app.user_id,app.update_time,app.create_time,app.name 
from app 
inner join user on app.user_id=user.id 
where app.name collate 'utf8mb4_general_ci' like CONCAT('%',#{name,jdbcType=VARCHAR},'%') or user.department like CONCAT('%',#{name,jdbcType=VARCHAR},'%') order by app.create_time desc,app.name