搜索从一日期到另一日期创建的记录。

huangapple go评论47阅读模式
英文:

search for records created from one date to another date

问题

我需要搜索从一日期到另一日期创建的记录。

$users = DB::table('users')
            ->whereDate('created_at', '>=', '2023-06-01')
            ->whereDate('created_at', '<', '2023-07-01')
            ->get();

我制作的查询仅在特定日期上搜索我,如何在两个日期之间搜索?

从2023-06-01到2023-07-01

英文:

i need to search for records created from one date to another date.

$users = DB::table(&#39;users&#39;)
                -&gt;whereDate(&#39;created_at&#39;, &#39;2023-06-01&#39;)
                -&gt;get();

The query that i make searches me only on a specific date, how can search between two dates?

2023-06-01 at 2023-07-01

答案1

得分: 1

如果您在查询中使用whereBetween(),它将为您提供存在于两个日期之间的数据,但不包括日期B上的数据。然而,如果您想要包括在日期B上创建的数据,您可以使用以下SQL表达式和whereRaw()

whereRaw('DATE(column_name) BETWEEN DATE(?) AND DATE(?)', [$dateA, $dateB])

这样,您将获得从日期A开始到包括日期B在内的所有数据范围内的数据。请记住将'column_name'替换为您要过滤的实际列,将$dateA$dateB替换为您感兴趣的具体日期。

请注意,使用whereRaw()允许您直接编写SQL表达式,因此请确保验证和清理输入,以防止任何SQL注入漏洞。

英文:

If you're using whereBetween() in your query, it will give you data that exists between two dates, excluding the data on date B. However, if you want to include the data created on date B as well, you can use whereRaw() with the following SQL expression:

whereRaw(&#39;DATE(column_name) BETWEEN DATE(?) AND DATE(?)&#39;, [$dateA, $dateB])

This way, you'll get all the data that falls within the range starting from date A up to and including date B. Remember to replace 'column_name' with the actual column you want to filter and $dateA and $dateB with the specific dates you are interested in.

Keep in mind that using whereRaw() allows you to directly write SQL expressions, so make sure to validate and sanitize the input to prevent any SQL injection vulnerabilities.

答案2

得分: 1

你可以使用 "whereBetween" 函数来完成:

use Carbon\Carbon;

$startDate = Carbon::createFromFormat('Y-m-d', '2016-06-01')->startOfDay();

$endDate = $startDate->addMonth(1);

$users = DB::table('users')
         ->whereBetween('created_at', [$startDate, $endDate])
         ->get();
英文:

You can do it using "whereBetween" function

use Carbon\Carbon;

$startDate = Carbon::createFromFormat(&#39;Y-m-d&#39;, &#39;2016-06-01&#39;)-&gt;startOfDay();
    
$endDate = $startDate-&gt;addMonth(1);
    
$users = DB::table(&#39;users&#39;)
         -&gt;whereBetween(&#39;created_at&#39;, [$startDate, $endDate])
         -&gt;get();

答案3

得分: 0

你可以使用以下方式使用 whereBetween 来完成:

$startDate = '2023-06-01';
$endDate = '2023-07-01';

$users = DB::table('users')
    ->whereBetween('created_at', [$startDate, $endDate])
    ->get();
英文:

You can do it using whereBetween in the following way:

$startDate = &#39;2023-06-01&#39;;
$endDate = &#39;2023-07-01&#39;;

$users = DB::table(&#39;users&#39;)
            -&gt;whereBetween(&#39;created_at&#39;, [$startDate, $endDate])
            -&gt;get();

huangapple
  • 本文由 发表于 2023年7月18日 11:03:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定