for the right syntax to use near ''(1266,1559,1764,2154,1512)' LIMIT 5' at line 1

对于

List<News> newsList_random =News.dao.find("select id,title,content,url from news WHERE id >= ROUND(((SELECT MAX(id) FROM news) - (SELECT MIN(id) FROM news)) * RAND()*500 + (SELECT MIN(id) FROM news)) AND id not in ? LIMIT 5",newsIdListQuery);

语句出现如下错误:

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 ''(1266,1559,1764,2154,1512)' LIMIT 5' at line 1

但是我在mysql执行是会出结果的,请教下这是什么问题呢?


评论区

JFinal

2019-03-19 10:01

not in 里面有五个参数,但是只用了一个问号占位字符

注意 not in 后面用好小括号

JFinal

2019-03-19 10:02

注意这是 JDBC 要求的,jfinal 没有干预过

liam_fang

2019-03-19 10:24

@JFinal 这里的问好的值也就是newsIdListQuery在这个里面就是类似(1266,1559,1764,2154,1512)的结构,这样是否会出问题呢?

JFinal

2019-03-19 11:10

@liam_fang JDBC 不支持你的用法, not in ( ? , ? , ? , ? , ?) 才支持你的五个参数

用 StringBuilder 来拼接 not in 后面的多个问号,可以写个工具类,根据参数个数自动拼接 not in 后面的问号

热门反馈

扫码入社