php - Laravel 5.2 中的 "IN"查询

标签 php mysql sql laravel

我正在尝试获取此查询:

$my_query = "select * from tableuser where `userid`<> '$userid' and userid in 
(select friend from userrelations where blocked='no' and user_id='$userid')";

Laravel 5.2 中工作

到目前为止我尝试过的:

$query_first = DB::table('userrelations')
                 ->select('friend')
                 ->where('blocked' , '=', 'no')
                 ->where('user_id' , '=', $userid)
                 ->get();

$my_query = DB::table('tableuser')
                 ->select('*')
                 ->where('userid' , '<>', $userid)
                 ->where('userid' , 'in', $query_first)
                 ->get();

通过这样做,我得到空结果,这是不正确的。

请问有什么帮助吗?

最佳答案

对于第一个查询,您应该替换

->get();

->pluck('id');

pluck()你将得到一个 id 数组而不是对象数组。

<小时/>

然后,第二个查询中的错误部分:

->where('userid' , 'in', $query_first)

whereIn() :

$users = DB::table('tableuser')->whereIn('userid', $query_first)->get();

关于php - Laravel 5.2 中的 "IN"查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37123960/

相关文章:

sql - 用日期表填补日期空白

php - 无法使用 AJAX 将数组数据从 View 传递到 Controller

php - 如何从购物车中获取多个产品到 Paypal 付款

mysql - MySQL 中的 Oracle 序列等效项

WHERE 子句中的 MySQL 变量

php - Laravel Report Analytics - 缓慢的 foreach 查询循环

php - Magento安装问题

php - 如何从谷歌日历获取事件详情

sql - 从 SQL 查询中删除重复的代码

mysql - 根据每个作业的最短距离获取单个对应行