如何在Laravel,子查询中进行此操作

时间:2013-05-29 13:42:07

标签: php laravel subquery laravel-3 where-in

如何在Laravel中进行此查询:

SELECT 
    `p`.`id`,
    `p`.`name`, 
    `p`.`img`, 
    `p`.`safe_name`, 
    `p`.`sku`, 
    `p`.`productstatusid` 
FROM `products` p 
WHERE `p`.`id` IN (
    SELECT 
        `product_id` 
    FROM `product_category`
    WHERE `category_id` IN ('223', '15')
)
AND `p`.`active`=1

我也可以通过连接来做到这一点,但我需要这种格式来提高性能。

11 个答案:

答案 0 :(得分:153)

考虑以下代码:

Products::whereIn('id', function($query){
    $query->select('paper_type_id')
    ->from(with(new ProductCategory)->getTable())
    ->whereIn('category_id', ['223', '15'])
    ->where('active', 1);
})->get();

答案 1 :(得分:44)

查看Fluent的高级文件:http://laravel.com/docs/queries#advanced-wheres

以下是您尝试实现的目标示例:

DB::table('users')
    ->whereIn('id', function($query)
    {
        $query->select(DB::raw(1))
              ->from('orders')
              ->whereRaw('orders.user_id = users.id');
    })
    ->get();

这将产生:

select * from users where id in (
    select 1 from orders where orders.user_id = users.id
)

答案 2 :(得分:18)

您可以使用关键字“use($ category_id)”

来使用变量
$category_id = array('223','15');
Products::whereIn('id', function($query) use ($category_id){
   $query->select('paper_type_id')
     ->from(with(new ProductCategory)->getTable())
     ->whereIn('category_id', $category_id )
     ->where('active', 1);
})->get();

答案 3 :(得分:4)

以下代码对我有用:

$result=DB::table('tablename')
->whereIn('columnName',function ($query) {
                $query->select('columnName2')->from('tableName2')
                ->Where('columnCondition','=','valueRequired');

            })
->get();

答案 4 :(得分:2)

该脚本在Laravel 5.x和6.x中进行了测试。在某些情况下,[ { id: 45193, amount: "8.79", date: 1579858773120, type: 11 }, { id: 45192, amount: "10.00", date: 1579858763947, type: 1 }, { id: 45191, amount: "2000.00", date: 1579858759085, type: 131 } ] 闭包可以提高性能。

static

生成SQL

Product::select(['id', 'name', 'img', 'safe_name', 'sku', 'productstatusid'])
            ->whereIn('id', static function ($query) {
                $query->select(['product_id'])
                    ->from((new ProductCategory)->getTable())
                    ->whereIn('category_id', [15, 223]);
            })
            ->where('active', 1)
            ->get();

答案 5 :(得分:0)

Laravel 4.2及更高版本,可以使用try relationship查询: -

Products::whereHas('product_category', function($query) {
$query->whereIn('category_id', ['223', '15']);
});

public function product_category() {
return $this->hasMany('product_category', 'product_id');
}

答案 6 :(得分:0)

Product::from('products as p')
->join('product_category as pc','p.id','=','pc.product_id')
->select('p.*')
->where('p.active',1)
->whereIn('pc.category_id', ['223', '15'])
->get();

答案 7 :(得分:0)

您可以在不同的查询中使用Eloquent,并使事情更易于理解和维护:

$productCategory = ProductCategory::whereIn('category_id', ['223', '15'])
                   ->select('product_id'); //don't need ->get() or ->first()

然后我们将它们放在一起:

Products::whereIn('id', $productCategory)
          ->where('active', 1)
          ->select('id', 'name', 'img', 'safe_name', 'sku', 'productstatusid')
          ->get();//runs all queries at once

这将生成与您在问题中编写的查询相同的查询。

答案 8 :(得分:0)

使用变量

$array_IN=Dev_Table::where('id',1)->select('tabl2_id')->get();
$sel_table2=Dev_Table2::WhereIn('id',$array_IN)->get();

答案 9 :(得分:0)

以下是我从多个答案中收集的 Laravel 8.x 方法:

Product::select(['id', 'name', 'img', 'safe_name', 'sku', 'productstatusid'])
    ->whereIn('id', ProductCategory::select(['product_id'])
        ->whereIn('category_id', ['223', '15'])
    )
    ->where('active', 1)
    ->get();

答案 10 :(得分:-1)

请尝试使用此在线工具sql2builder

DB::table('products')
    ->whereIn('products.id',function($query) {
                            DB::table('product_category')
                            ->whereIn('category_id',['223','15'])
                            ->select('product_id');
                        })
    ->where('products.active',1)
    ->select('products.id','products.name','products.img','products.safe_name','products.sku','products.productstatusid')
    ->get();