这篇文章主要介绍“weed3-2.3.2.查询的条件是什么”,在日常操作中,相信很多人在weed3-2.3.2.查询的条件是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”weed3-2.3.2.查询的条件是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
源码:https://github.com/noear/weed3 源码:https://gitee.com/noear/weed3
单表条件查询(有了简单的自然能拼成复杂的)
//weed3 的条件构建,是相当自由的
String mobile = "111";
db.table("test")
.where("mobile=?",mobile).and().begin("sex=?",1).or("sex=2").end()
.limit(20)
.select("*").getMapList()
db.table("test")
.where("mobile=?",mobile).and("(sex=? OR sex=2)",1)
.limit(20)
.select("*").getMapList()
db.table("test").where("mible=? AND (sex=1 OR sex=2)",mobile)
.limit(20)
.select("*")
//以上三种,效果是一样的。。。因为很自由,所以很容易使用(也有观点认为:所以很难控制)
有时候一些条件需要动态控制
//这个示例,管理后台很常见
int type=ctx.paramAsInt("type",0);
String key=ctx.param("key");
int date_start=ctx.paramAsInt("date_start",0);
int date_end=ctx.paramAsInt("date_end",0);
DbTableQuery qr = db.table("test").where("1=1");
if(type > 0){
qr.and("type=?", type);
}
if(key != null){
qr.and('"title LIKE ?",key+"%");
}
if(date_start>0 && date_end >0){
qr.and("( date >=? AND date<=? )", date_start, date_end);
}
qr.select("id,title,icon,slug").getMapList();
多表关联查询:innerJoin(..), leftJoin(..), rightJoin(..)
//innerJoin()
db.table("user u")
.innerJoin("user_book b").on("u.id = b.user_id")
.select("u.name,b.*")
//leftJoin()
db.table("user u")
.leftJoin("user_book b").on("u.id = b.user_id").and("u.type=1")
.select("u.name,b.*")
//rightJoin()
db.table("user u")
.rightJoin("user_book b").on("u.id = b.user_id")
.where("b.type=1").and("b.price>",12)
.select("u.name,b.*")
想别的关联查询怎么样?(如:full join)
//因为不是所有的数据库都支持 full join,所以...
db.table("user u")
.append("FULL JOIN user_book b").on("u.id = b.user_id")
.select("u.name,b.*")
//.append(..) 可以添加任何内容的接口
到此,关于“weed3-2.3.2.查询的条件是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/noear/blog/3121507