我有一个mysql查询,我需要使用Or检查2列中的值数组。 Inshort我有一个数组,需要使用Or条件在2列上进行检查。
SELECT *
FROM table_name
WHERE col_1 IN (1, 8, 3)
OR
WHERE col_2 IN (1, 8, 3)
$mu = DB::table('users')
->join('table_a', 'users.id', '=', 'table_a.user_id')
->select('table_a.user_id')
//This are all AND
->where('table_b.search_status','available')
->where('table_b.role','<>',2)
//This 2 parts needs to be in OR clause
->whereIn('table_b.lang',$seeks)
->whereIn('table_b.lang_2',$seeks)
//This is AND too
->whereIn('table_a.language_id',$lang)
->get()->pluck('user_id')->toArray();
我想做类似的事情:
->where(function($q)
{
$q->whereIn('table_b.lang',$seeks)
->orWhere(whereIn('table_b.lang_2',$seeks));
})
但是给出错误:undefined $ seek。
答案 0 :(得分:1)
使用封闭条件来封装它们。
$mu = DB::table('users')
->join('table_a', 'users.id', '=', 'table_a.user_id')
->select('table_a.user_id')
->where('table_b.search_status', 'available')
->where('table_b.role','<>', 2)
->where(function ($query) use ($seeks) {
$query->whereIn('table_b.lang', $seeks)
->orWhereIn('table_b.lang_2', $seeks);
})
->whereIn('table_a.language_id', $lang)
->get()->pluck('user_id')->toArray();
答案 1 :(得分:0)
只需替换它: -
->where(function($q) use ($seeks)
{
$q->whereIn('table_b.lang',$seeks)
->orWhere(whereIn('table_b.lang_2',$seeks));
});
向
->where(function($q) use ($seeks)
{
$q->whereIn('table_b.lang',$seeks)
->orWhere(whereIn('table_b.lang_2',$seeks));
});