赞
踩
想用QueryWrapper返回的结果作为查询的条件,想实现SQL语句为:
SELECT x,x,x,x,x,x
FROM data
WHERE (region = ? AND (code = ? OR tail_number = ? ))
错误写法:
QueryWrapper<Expressdata> qw = new QueryWrapper<>();
qw.eq("region");
qw.eq("code", word).or().eq("tail_number", word);
这样对同一字段连续进行两次eq,其虽然把两个条件叠加查询了,但是其SQL语句为:
SELECT x,x,x,x,x,x
FROM data
WHERE region = ? AND code = ? OR tail_number = ?
在SQL中由于AND的优先级比OR要高,故此语句会执行:
(region = ? AND code = ?)OR tail_number = ?
正确的and()和or()连用用法:
QueryWrapper<Expressdata> qw = new QueryWrapper<>();
QueryWrapper<Expressdata> querywrapper;//定义一个变量存放最终结果
querywrapper=qw.eq("region").and(qw2->qw2.eq("code", word).or().eq("tail_number", word));
这样才可以正确的运行:
SELECT x,x,x,x,x,x
FROM data
WHERE (region = ? AND (code = ? OR tail_number = ? ))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。