thinkphp5使用model查询数据时exists作为条件的用法

发布时间:2020/06/07 作者:天马行空 阅读(3880)

在thinkphp框架中如果使用model查询数据,有时候需要用到exists作为条件,具体代码应该怎么写呢?下面我们就来看看。


假设我们的需求是要查询今天有登陆过系统的用户,直接写sql的话应该是:

select * from user where exists (
    select id from user_login_log where user_id=user.id and login_time>='2020-06-07 00:00:00'
);


那么,转变成用thinkphp框架就应该写成这样:

<?php
$where = [
    ['', 'exists', Db::raw('select id from user_login_log where user_id=user.id and login_time>='2020-06-07 00:00:00')]
];
$result = UserModel::where($where)->select();
?>


关键字thinkphp sql