Display MySQL results by date

前端 未结 5 1544
眼角桃花
眼角桃花 2020-12-01 20:22

I have this mysql Table:

+--------------------+---------+-------+
|      date          | query   | count |
|--------         


        
相关标签:
5条回答
  • 2020-12-01 20:59

    What about

     SELECT * FROM my_tables GROUP BY YEAR(date), MONTH(date), DAY(date) ORDER BY date DESC LIMIT 20
    
    0 讨论(0)
  • 2020-12-01 21:05

    Here is teh PHP code:

    $query = mysql_query("SELECT date, query FROM table6 ORDER BY date DESC LIMIT 20");
    $group_date = null;
    while ($row = mysql_fetch_assoc($query)) {
        if ($group_date !== substr($row["date"], 0, 10)) {
            $group_date = substr($row["date"], 0, 10);
            echo "<h1>$group_date</h1>\n";
        }
        echo "${row['query']}<br>\n";
    }
    

    Output:

    2012-11-18

    Tom

    Michael

    2012-11-17

    Erik

    John

    2012-11-16

    Larry

    Kate

    Note that while this code "groups" rows by one column, it can easily be extended to group rows by multiple columns. Left as an exercise.

    0 讨论(0)
  • 2020-12-01 21:05

    Try the WHERE statement:

       select * from my_table where date_column > "2012-11-18 00:00:00" and date_column < "2012-11-18 23:59:59" order by date_column
    
    0 讨论(0)
  • 2020-12-01 21:07

    +1 good question, THIS QUESTION IS NOT SIMPLY HOW TO ORDER A QUERY!!

    I think this can be done using GROUP_CONCAT function. this will combine the matching results when you group them and seperate them by commas. ONce you have the results you can explode or do whatever you want

    http://www.w3resource.com/mysql/aggregate-functions-and-grouping/aggregate-functions-and-grouping-group_concat.php

    Your going to have to convert and group by date, and group_concat the names. then (in php or whathaveyou) explode the names by commas, and echo the date followed by the names for each result.

    edit Looks like you wanted a PHP way to solve this? oops

    0 讨论(0)
  • 2020-12-01 21:09

    Use this:

    SELECT * FROM my_tables WHERE date > "2012-11-16" ORDER BY date LIMIT 4
    
    0 讨论(0)
提交回复
热议问题