tp5的数据查询语言
1.查询一句
$result = Db::name("log")
->where("id", 2)
->find();2.按条件查询多条语句
//多条件查询
//中间参数可以写成 >= <= <> in [4,5,6,7] "between" [5,8]
$result = Db::name("log")
->where("id", ">=", 2)
->select();
$result = Db::name("log")
->where("id", "in", [102,103,104])
->select();3.使用exp天条件表达式,表示后面是原生的SQL语句表达式
$result = Db::name("log")
->where("id", "exp", ">1 and user_id = 1")
->select();4.使用多个字段的查询
$result = Db::name("log")
->where("id", ">=", "1")
->where("user_id", "1")
->select();
或者:
$result = Db::name("log")
->where([
"id" => [">=", 1],
"ip" => ["like", "%1%"],
])->select();5.带有or或and的查询
$result = Db::name("log")
->where("ip", "like", "%1%")
->where("id", ["in", [1,2,3]], [">=", 1], "or")
->limit(2)
->select();6. 快捷查询
//如果要查询id和user_id同时大于1的的项,可以像下面这样写
$result = Db::name("log")
->where("id&user_id", ">", 1)
->limit(10)
->select();
//如果要查询id大于1或者user_id大于1的的项,可以像下面这样写
$result = Db::name("log")
->where("id|user_id", ">", 1)
->limit(10)
->select();7. 获取某行某列的某个值
//获取某行
+的某个值
$name = Db::name("log")
->where("id", 102)
->value("user_id");
//获取某列的某个值
$name = Db::name("log")
->where("user_id", 1)
->column("ip");
$result = $name;8. 聚合查询
$count = Db::name("log")->where("user_id", 1)->count();9. scope + 查询范围名称
在model中的代码
protected function scopeIp($query){
$query->where("ip", "10.10.11.11");
}
protected function scopeUserId($query){
$query->where("user_id", 1);
}声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: thinkPHP的数据添加、修改、删除
- 下一篇: tp5数据验证及验证场景详解
