简体   繁体   中英

Group by date range in mysql and php

I have a regular table for posts, and it has a datetime field. I want to get posts in specific date ranges.

Eg I want all posts that were created between Nov 10, 2019 and Nov 13, 2019 as a single group of posts, and another group of posts that were created in other date range, like between Jan 1, 2019 and Jan 15,2019 .

What I would like is to have something like this:

[
"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'],...],
...
]

Is it possible using a single query?

I know I can group them together using something like GROUP BY DATE(dateTimeField) .

But how can I group it by date range?

EDIT : what I currently have:

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

this is my query and i know i can use BETWEEN for this purpose but I'm considering that's not the question.

the question is how can I group by the range like this:

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

i DON'T want this:

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)

i dont want to separate posts based on each individual days but based on the same range I have specfied.

Assuming that your table is named TableX and the field containing post dates is named post_date see the table below with 3 columns:

+----+------------+-------------+
| 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  |
+----+------------+-------------+

To get the posts grouped by date range you will run the following PHP/MySQL script:

//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>";

This will return the following multi-dimensional array

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
                )

        )

)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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