简体   繁体   English

如何在SQL查询中使用带有%LIKE%的WhereBetween日期

[英]How to use WhereBetween date with %LIKE% in sql query

In my database creadted_at data 2017-11-07 18:58:16,2017-11-07 19:58:16 . 在我的数据库中,creadted_at数据2017-11-07 18:58:16,2017-11-07 19:58:16 I try to use WhereBetween for searching data in date 2017-11-07 Am not sure How can I put Like % % into my query 我尝试使用WhereBetween搜索日期2017-11-07中的数据我不确定如何将Like % %放入查询中

 'like', '%'.2017-11-07.'%'

 ->WhereBetween('created_at', ['2017-11-07', '2017-12-07'])

Here is my full controller 这是我的全职控制器

  $b = DB::table("v_dealer_sell_time")
             ->select([
                    'product_name',
                    DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
                    DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
                    ])
            ->WhereBetween('created_at', ['2017-11-07', '2017-11-07'])
            ->groupBy('product_name')
            ->get();

Use whereDate() : 使用whereDate()

$date = Carbon::parse($date)->toDateString();
....
->whereDate('created_at', $date)

Or whereBetween() : whereBetween()

$date = Carbon::parse($date);
$from = $date->copy()->startOfDay();
$to = $date->copy()->endOfDay();
....
->whereBetween('created_at', [$from, $to])

If you want the created ones in the same given date you can use whereDate like this But since 5.3 ( Documentation ): 如果您希望在相同的给定日期创建日期,则可以使用whereDate这样,但是从5.3开始( 文档 ):

$b = DB::table("v_dealer_sell_time")
             ->select([
                    'product_name',
                    DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
                    DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
                    ])
            ->WhereDate('created_at', '=', $date)
            ->groupBy('product_name')
            ->get();

If you want it from between two dates use whereBetween like this : 如果要在两个日期之间使用它,请使用whereBetween,如下所示:

$b = DB::table("v_dealer_sell_time")
             ->select([
                    'product_name',
                    DB::raw('COALESCE(SUM(total_price), 0) AS total_price'),
                    DB::raw('COALESCE(SUM(total_product), 0) AS total_product')
                    ])
            ->WhereBetween('created_at', [Carbon::parse('2017-11-07')->startOfDay(), Carbon::parse('2017-12-07')->endOfDay()])
            ->groupBy('product_name')
            ->get();

PS : Do not forget to add use Carbon\\Carbon; PS:不要忘记添加use Carbon\\Carbon; at the top of your Controller. 在您控制器的顶部。

If this Date search related to a Search Function, then you have to do something like this 如果此日期搜索与搜索功能相关,那么您必须执行类似的操作


Code

Change this to 更改为

->WhereBetween('created_at', ['2017-11-07', '2017-11-07'])

this 这个

->WhereBetween('created_at', ['2017-11-07 00:00:00', '2017-11-07 23:59:59'])

WHY ?? 为什么?

2017-11-07 00:00:00 - Start of the day 2017-11-07 00:00:00一天的开始
2017-11-07 23:59:59 - end of the day 2017-11-07 23:59:59 : 2017-11-07 23:59:59一天结束


if you use tosql() and check your query it has something like this 如果您使用tosql()并检查查询,则它具有类似以下内容

.`created_at` between ? and ? "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM