【问题标题】:How can I add condition in db raw laravel?如何在 db raw laravel 中添加条件?
【发布时间】:2018-03-15 12:00:52
【问题描述】:

我的查询数据库原始 laravel 是这样的:

public function getTopProduct($price = null) {
    $products =  DB::select(DB::raw('SELECT * 
                    FROM (
                        SELECT a.*, b.name AS store_name, b.address
                        FROM products a
                        JOIN stores b ON b.id = a.store_id
                        WHERE a.status = 1                                                
                    ) AS product
                    GROUP BY store_id')
        );
    return $products;
}

我要加条件

如果价格不为空,则会在where上添加条件价格

比如price = 1000,那么在where上查询如下:

WHERE a.status = 1 AND a.price < 1000

如果 price = null,则条件 AND a.price &lt; 1000 未执行

我该怎么做?

更新

我稍微改变了我的代码流程

我尝试这样:

public function getTopProduct($price)
{
    if($price == 1)
        $price_condition = 'WHERE price > 1000';
    else if($price == 2)
        $price_condition = 'WHERE price >= 500 AND a.price <= 1000';
    else if($price == 3)
        $price_condition = 'WHERE price < 500';
    else
        $price_condition = '';


    $products = DB::select('SELECT * 
                    FROM (
                        SELECT a.*, b.name AS store_name, b.address
                        FROM products a
                        JOIN stores b ON b.id = a.store_id
                        WHERE a.status = 1                                                
                    ) AS product
                    GROUP BY store_id
                    '.$price_condition
                );
    return $products;
}

它有效

你觉得怎么样?

我的解决方案是否正确?或者您有更好的解决方案?

【问题讨论】:

  • $price=array_column($request-&gt;get('price')); 取一个变量并以where 条件进行-&gt;when 查询!它在请求价格时执行!

标签: mysql laravel laravel-5 laravel-5.3 laravel-eloquent


【解决方案1】:

使用where 条件尝试以下代码:

$price = array_column($request->get('price'), 'id');

$price = $request-&gt;get('price');

$query = DB::select(DB::raw('SELECT * 
                FROM (
                    SELECT a.*, b.name AS store_name, b.address
                    FROM products a
                    JOIN stores b ON b.id = a.store_id
                    WHERE a.status = 1                                                
                ) AS product
                GROUP BY store_id'))
             ->when($price == 1, function($query) use ($price) {
                  $query->where('price', '>', '1000');
             })
             ->when($price == 2, function($query) use ($price) {
                  $query->where('price', '>=', '500')
                        ->where('a.price', '<=', '1000');
             })
             ->when($price == 3, function($query) use ($price) {
                  $query->where('price', '<', '500');
             })
             ->when($price == '', function($query) use ($price) {
                  $query->where('your_where_when_price_empty');
             })
             ->get();

希望对您有所帮助!

【讨论】:

  • 如果使用DB:raw会怎样?
  • 这似乎对我没有帮助。试试看我的情况。 Select、from、group by 在 db:raw 中。我想在 db:raw 中添加条件
  • 你可以在 DB::raw 中做到这一点!将您的 DB::raw 代码放在我的查询中并尝试一下!我认为这是工作。
  • 您尝试根据我的情况更新您的答案。因为我仍然对你的回答感到困惑
  • 这个:DB::table('table_name')。似乎没有必要。应该DB::select......。还有这个:$query-&gt;where('a.status = 1');。应该$query-&gt;where('a.price&lt; 1000');
【解决方案2】:
public function getTopProduct($price = null) {
if($price==null){
         $products =  DB::select(DB::raw('SELECT * 
                FROM (
                    SELECT a.*, b.name AS store_name, b.address
                    FROM products a
                    JOIN stores b ON b.id = a.store_id
                    WHERE a.status = 1                                                
                ) AS product
                GROUP BY store_id')
    );
 }else{
       $products =  DB::select(DB::raw('SELECT * 
                FROM (
                    SELECT a.*, b.name AS store_name, b.address
                    FROM products a
                    JOIN stores b ON b.id = a.store_id
                    WHERE a.status = 1 AND a.price='.$price.'                                                
                ) AS product
                GROUP BY store_id')
    );
 }
 return $products;
}

【讨论】:

  • 可以在DB:raw里面添加条件吗?因为你的代码太长了
  • 是的,这将是 sql 条件而不是 php 条件
【解决方案3】:

您可以在查询生成器中使用Advanced Join Clauses

DB::table('products')
        ->join('stores', function ($join) use ($price) {
            $query = $join->on('stores.id', '=', 'products.store_id')
                          ->where('products.status', '=', 1);

            if (!empty($price)) {
                $query->where('products.price', '<', 1000);
            }
        })
        ->select('products.*', 'stores.name AS store_name', 'stores.address')
        ->groupBy('products.store_id')
        ->get();

【讨论】:

  • 如果像我的情况一样使用DB:raw,如何添加条件?
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2016-07-29
  • 2018-10-24
  • 2019-01-10
  • 1970-01-01
  • 2015-03-26
  • 2019-11-28
  • 2015-11-20
相关资源
最近更新 更多