开发者

What is the best way to go about grouping rows by the same timestamp?

开发者 https://www.devze.com 2023-01-02 02:52 出处:网络
Right due to not getting the right answ开发者_运维百科er last time, I will reword this. I want to create a page where all fixtures from \'tbl_fixtures\' are shown.

Right due to not getting the right answ开发者_运维百科er last time, I will reword this.

I want to create a page where all fixtures from 'tbl_fixtures' are shown.

id - compname - home_user - home_team - away_user - away_team - date

That is the table setup. The date column is a timestamp. I want to show all fixtures in blocks based on the date column.

There will be blocks of 10 fixtures with the same timestamp on them. I want to show it like this...

Fixture 1 - (timestamp in readable format) Then all 10 fixtures....

Fixture 2 - (timestamp) Then 10 fixtures...

etc etc.

Until all the fixtures are grouped by their timestamp.

How could I go about doing this?


If you want to "group" them by weeks you can use the WEEK() function. The best would be to order the rows by the timestamp and print a new week header if the week changes:

$result = $mysql_query("SELECT WEEK(timestamp) as week, * FROM myTable ORDER BY timestamp");

$current_week = null;
while ($row = mysql_fetch_assoc($result)) {
  if ($row["week"] != $current_week) {
    $current_week = $row["week"];
    echo 'Week ' . $current_week .': ';
  }
  echo $row["timestamp"];
  // echo other fields
}


I'll try to read your mind and suggest using DATE_FORMAT:

SELECT DATE_FORMAT(`add_dt`, '%u') AS `d`, `field1`, `field2`
FROM `table1`
ORDER BY `d`

Where %u is week number (ref).

0

精彩评论

暂无评论...
验证码 换一张
取 消