php - mysql 和 php 中按日期范围分组

标签 php mysql

我有一个常规的帖子表,它有一个日期时间字段。我想要获取特定日期范围内的帖子。

例如我希望将2019年11月10日2019年11月13日之间创建的所有帖子作为一组帖子,以及在其他日期范围内创建的另一组帖子,例如 2019 年 1 月 1 日2019 年 1 月 15 日 之间。

我想要的是这样的东西:

[
"Nov 10,2019-Nov 13,2019"  =>  [['id'=>1,'text'=>'something'],['id'=>2,'text'=>'something'],...],
"Jan 1,2019-Jan 15,2019"  =>  [['id'=>1,'text'=>'something'],['id'=>2,'text'=>'something'],...],
...
]

是否可以使用单个查询?

我知道我可以使用诸如GROUP BY DATE(dateTimeField)之类的方法将它们分组在一起。

但是如何按日期范围对其进行分组?

编辑:我目前拥有的:

SELECT * FROM posts WHERE ( (datetime >= '2019-11-10 00:00:00' AND datetime <= '2019-11-13 23:59:59') OR (datetime >= '2019-01-01 00:00:00' AND datetime <= '2019-01-15 23:59:59')) LIMIT 0,50

这是我的查询,我知道我可以使用 BETWEEN 来实现此目的,但我认为这不是问题。

问题是我如何按这样的范围进行分组:

SELECT * FROM posts WHERE ( (datetime >= '2019-11-10 00:00:00' AND datetime <= '2019-11-13 23:59:59') OR (datetime >= '2019-01-01 00:00:00' AND datetime <= '2019-01-15 23:59:59')) LIMIT 0,50 GROUP BY THE_RANGE_SPECIFIED_IN_WHERE_CLAUSE

我不想要这个:

SELECT * FROM posts WHERE ( (datetime >= '2019-11-10 00:00:00' AND datetime <= '2019-11-13 23:59:59') OR (datetime >= '2019-01-01 00:00:00' AND datetime <= '2019-01-15 23:59:59')) LIMIT 0,50 date(datetime)

我不想根据每一天来分隔帖子,而是根据我指定的相同范围来分隔帖子。

最佳答案

假设您的表格名为 TableX,并且包含发布日期的字段名为 post_date,请参阅下表,其中包含 3 列:

+----+------------+-------------+
| id | post_date  | text        |
+----+------------+-------------+
|  1 | 2019-11-10 | xsomething  |
|  2 | 2019-11-10 | ysomething  |
|  3 | 2019-11-11 | ysomething  |
|  4 | 2019-11-12 | ysomething  |
|  5 | 2019-11-13 | xysomething |
|  6 | 2019-01-01 | xysomething |
|  7 | 2019-01-05 | xysomething |
|  8 | 2019-01-06 | ysomething  |
|  9 | 2019-01-10 | xsomething  |
| 10 | 2019-01-11 | ysomething  |
+----+------------+-------------+

要获取按日期范围分组的帖子,您将运行以下 PHP/MySQL 脚本:

//Query databases using CASE,THEN
$sql1 = "SELECT id AS post_id,text,post_date,  
    CASE 
        WHEN post_date BETWEEN '2019-11-10' AND  '2019-11-13' 
    THEN 1 
        WHEN post_date BETWEEN '2019-01-01' AND  '2019-01-15' 
    THEN 2 
        Else 1 END AS date_range_id FROM posts";
$sth = $con->prepare($sql1);
    $sth->execute();
    $sth->SetFetchMode(PDO::FETCH_ASSOC);
    while ($row=$sth->fetch()){
        $date_range_id = $row['date_range_id'];

        if($date_range_id == 1){
            $post_id = $row['post_id'];
            $text = $row['text'];
            $post_date = $row['post_date'];

            $array[] = [$post_id,$text,$post_date];
        }
        elseif($date_range_id == 2){
            $post_id = $row['post_id'];
            $text = $row['text'];
            $post_date = $row['post_date'];
            $array5[] = [$post_id,$text,$post_date];
        }

    }
$x= 1;
//create an associative array  $q of $array[]
foreach($array as $y => $z){
    $q[] = ['id' => $x++,
        'post_id'=>$z[0],
        'text' =>$z[1],
        'post_date' => $z[2]
];

}

//create an associative array $r of $array5[]
$l= 1;
foreach($array5 as $y => $z){
    $r[] = ['id' => $l++,
        'post_id'=>$z[0],
        'text' =>$z[1],
        'post_date' => $z[2]];
}

$post_range = [
    "'2019-11-10' AND '2019-11-13'" => $q,
    "'2019-01-01' AND '2019-01-11'" => $r];
echo "<pre>";print_r($post_range);echo "</pre>";

这将返回以下多维数组

Array
(
    ['2019-11-10' AND '2019-11-13'] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [post_id] => 1
                    [text] => xsomething
                    [post_date] => 2019-11-10
                )

            [1] => Array
                (
                    [id] => 2
                    [post_id] => 2
                    [text] => ysomething
                    [post_date] => 2019-11-10
                )

            [2] => Array
                (
                    [id] => 3
                    [post_id] => 3
                    [text] => ysomething
                    [post_date] => 2019-11-11
                )

            [3] => Array
                (
                    [id] => 4
                    [post_id] => 4
                    [text] => ysomething
                    [post_date] => 2019-11-12
                )

            [4] => Array
                (
                    [id] => 5
                    [post_id] => 5
                    [text] => xysomething
                    [post_date] => 2019-11-13
                )

        )

    ['2019-01-01' AND '2019-01-11'] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [post_id] => 6
                    [text] => xysomething
                    [post_date] => 2019-01-01
                )

            [1] => Array
                (
                    [id] => 2
                    [post_id] => 7
                    [text] => xysomething
                    [post_date] => 2019-01-05
                )

            [2] => Array
                (
                    [id] => 3
                    [post_id] => 8
                    [text] => ysomething
                    [post_date] => 2019-01-06
                )

            [3] => Array
                (
                    [id] => 4
                    [post_id] => 9
                    [text] => xsomething
                    [post_date] => 2019-01-10
                )

            [4] => Array
                (
                    [id] => 5
                    [post_id] => 10
                    [text] => ysomething
                    [post_date] => 2019-01-11
                )

        )

)

关于php - mysql 和 php 中按日期范围分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58856118/

相关文章:

php - 以 POST 请求发送删除操作

php - Cron 作业执行 php 文件

sql - MYSQL SUM GROUP BY

php - CodeIgniter 和多语言数据库

php - 在原始 DOMDocument 被销毁后访问导入的元素

javascript - 替换html字符串中的标签

php - 图片删除后重定向回来

php - 需要帮助弄清楚为什么我会收到以下错误 : Fatal error: Call to a member function update() on a non-object in

javascript - 如何将单个 SQL OR 运算符应用于 Sequelize 中的表及其关联表?

sql - 强制 mysql 在 bit_or 上不返回结果