问题点:
String guestId_filter = "0001"StringBuilder sql = new StringBuilder("select m from Message m where 1=1 ");String condition = " and find_in_set(" + guestId_filter + ", guestids)>0";sql.append(condition);System.out.PRintln("sql = " + sql.toString());打印的结果为 sql = select m from Message m where 1=1 and find_in_set(0001, guestids)>0
数据中有guestId 为0001这条记录,但用MySQL 命令死活查不出来,如果将guestId_filter改为1000,又可以查找到guestId为1000的记录,到底是为啥呢?特么纠结了一个晚上!
分析中: 原来当这条语句传给mysql 执行时,0001这个参数被强转成整型1, 所以这条命令就变成了
`sql = select m from Message m where 1=1 and find_in_set(1, guestids)>0`所在当然就检索不到0001这条记录了。。。。。。。。
解决方法: 将guestId_filter用单引号(”)括起来即可
String guestId_filter = "0001"StringBuilder sql = new StringBuilder("select m from Message m where 1=1 ");String condition = " and find_in_set('" + guestId_filter + "', guestids)>0";sql.append(condition);System.out.println("sql = " + sql.toString());打印的结果为 sql = select m from Message m where 1=1 and find_in_set('0001', guestids)>0
即可成功查询到guestid为0001这条记录。
新闻热点
疑难解答