2

I had a mysql table called events with the fields: id, date, and name. The date field has the format yyyy-mm-dd hh::mm:ss edit: meaning it is in datetime format

I want to group the events by day, and I wasn't sure how to approach this- is there a way to select only the month and day from the field? or should i use PHP after I select all the "events"

my end goal is to have something like this:

March 10th: 
  event1, 
  event2
March 11th: 
  event4, 
  event5

I found MySQL select using datetime, group by date only but I'm not sure how to implement it:

SELECT DATE_FORMAT(date, '%H%i'), DATE_FORMAT(date, '%M %D'), name FROM events ORDER BY date

Thanks!

EDIT:

ended up using this:

$sql = "select team1, team2, DATE_FORMAT(date,'%Y-%m-%d') as created_day FROM games WHERE attack = '1' GROUP BY created_day";
    $result = mysql_query($sql);
    $curDate = "";

    while (list($team1, $team2, $date) = mysql_fetch_row($result))
    {
      if ($date != $curDate)
      {
        echo "$date --------\n";
        $curDate = $date;
      }

      echo "game data: $team1 $team2";
    }
Shehary
  • 9,926
  • 10
  • 42
  • 71
jaredrada
  • 1,130
  • 3
  • 12
  • 25
  • What is the actual date type? I mean, is it (I hope) a datetime or timestamp, or do you mean that it is a string formatted as you say? – Nanne Mar 14 '12 at 22:09
  • Your edit has `GROUP BY created_day` on the end of the SQL. You need to remove that otherwise it will only return one row per date. You can use ``DATE(`date`)`` instead of ``DATE_FORMAT(`date`,'%Y-%m-%d')``. – user1191247 Mar 14 '12 at 23:14

4 Answers4

2

If you use group by you will not get one row out of it. So the way you want is not possible through Group By AFAIK.

$query = "SELECT distinct(DATE_FORMAT(date, '%M %D')) as d FROM yourtable";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
   echo $row['d']
   $sql = "SELECT * FROM yourtable WHERE DATE_FORMAT(date, '%M %D')='$row[d]'";
   $rs = mysql_query($query);
   while($r = mysql_fetch_assoc($rs)) {   
      echo "event";
   }
}
Starx
  • 77,474
  • 47
  • 185
  • 261
1

You should indeed use php to get this done. But since most of current system sepate logic from display, I'd use only one pass and not (NUMBER OF DAYS + 1) SELECTs, and prepare an array that I can reuse later for my display.

$query = "SELECT DATE_FORMAT(date, '%M %D') as d, name FROM yourtable ORDER BY date";
$foo=array();
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)) {
    //some logic to test if it's safe to add the name
    $foo[$row['d']][]=$row['name'];
}

And then when i'd need it (through a template or your "view")

foreach($foo as $date => $events) {
    echo $date . ":\n\t";          
    echo implode(",\n\t", $events);
    echo "\n";
}

so it fits the format you set to yourself.

Hope that helped

Shehary
  • 9,926
  • 10
  • 42
  • 71
Kharaone
  • 597
  • 4
  • 16
0

I think from that question/answer, you can get something like this instead

March 10th, event1
March 10th, event2
March 11th, event4
March 11th, event5

It does not really 'group' dates as your wish but I think you can use php to continue from this result.

Surasin Tancharoen
  • 5,520
  • 4
  • 32
  • 40
0

I agree with Kharaone, separate logic from display. That being said, I think that something similar to this query might be what you are looking for:

SELECT A FROM 
(
    SELECT DATE_FORMAT(date,'%M %D:') AS A, DATE(date) AS B, 1 AS C FROM games GROUP BY DATE(date)
        UNION ALL
    SELECT name AS A, DATE(date) AS B, 2 AS C FROM games
) X
ORDER BY B, C ASC;
Francisco Paulo
  • 6,284
  • 26
  • 25