php - mysql查询返回所有数据,即使where不满足LARAVEL

标签 php mysql laravel

我刚刚注意到我的 where status =Blocked 没有被考虑。
查询仍返回阻止和事件状态。

$contacts = DB::table('contacts')
        ->where('uid_by', $me)
        ->orWhere('uid_to', $me)
        ->where('status', 'blocked')
        ->select(
                DB::raw("IF(uid_by = $me, uid_to, uid_by) AS user_id")
        )
        ->get();

查询可能出现什么问题?

我尝试记录正在执行的查询:

{"query":"select IF(uid_by = 3, uid_to, uid_by) AS user_id from `contacts` where `uid_by` = ? or `uid_to` = ? and `status` = ?","bindings":[3,3,"blocked"],"time":0}

使用:

$queries = DB::getQueryLog();
return $last_query = end($queries); 

最佳答案

尝试切换 where 语句,如下所示:

$contacts = DB::table('contacts')
    ->where('uid_by', $me)
    ->where('status', 'blocked')
    ->orWhere('uid_to', $me)
    ->select(
        DB::raw("IF(uid_by = $me, uid_to, uid_by) AS user_id")
    )->get();

发生的情况是,没有评估 OR 关键字后面的内容,因此,显示所有被阻止和事件的...

关于php - mysql查询返回所有数据,即使where不满足LARAVEL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20803227/

相关文章:

java - 为什么 PrepareStatement 没有生成正确的 SQL 查询?

mysql - 在 MySQL 中对 Union 的两个部分求和有问题

Laravel 9 - 如何防止在用户登录并点击浏览器后退按钮后显示登录页面

php - Laravel 邮件中的 "Reply-to"字段不起作用

php - 如何修复此错误 XML 解析错误 : not well-formed

php - 如何在 getopt() 之前修改命令行参数

mysql - sql中的组合不重复

php - Composer 安装要求

php - 如何将值从 View 传递到模态?

php - mysql 查询 wp_post 列表和类别和日期