dongyao8698 2018-06-01 20:32
浏览 71
已采纳

如何按日期列出记录?

you see I am trying to list the records of a table in my database, but I want to do it in the following way:

  1. First, it has to display the date
  2. and then all the records on that date should appear

In my table of the database I have 4 fields:

id, task, date, time

For example there are multiple tasks that are performed in a day, but at different times. Then I have stored in my database many tasks of different days and in different hours. What I want is to list them per day. Consult the database and show a list where the date appears first and then all the tasks that were done on that date, then show the other date and then all the tasks of that date and so on.

Something like that

That's my php code

$obj = new Task();
$consult = $obj->Lists();

date_default_timezone_set("America/Mexico_City");
$dateActual = date("Y-m-d");

while ($result = $consult->fetch_object()) {

    echo "<button class='btn btn-default'>date = " . $result->date . "</button><br>";

    $consult2 = $obj->Lists2($dateActual);
    while($result2 = $consult2->fetch_object()) {
          echo "<span>". $result2->time ."</span><br>";
    }
    $dateActual = $result->date;
}

my query to the database is:

public function Lists2($date) 
{
    global $conexion;

    $sql = "SELECT ar.*, date_format(ar.date, '%d/%m/%Y') as date, 
                   date_format(ar.time, '%r') as time, 
                    u.user as User 
            FROM task_recents ar 
                INNER JOIN user u ON ar.iduser = u.iduser 
            WHERE date = '$date' 
            ORDER BY ar.time DESC";
    $query = $conexion->query($sql);
    return $query;
}

public function Lists() 
{
    global $conexion;

    $sql = "SELECT ar.*, date_format(ar.date, '%d/%m/%Y') as date, 
                    date_format(ar.time, '%r') as time, 
                    u.user as User 
            FROM task_recents ar 
                INNER JOIN user u ON ar.iduser = u.iduser 
            ORDER BY ar.time DESC";
    $query = $conexion->query($sql);
    return $query;
}

The result is that it shows me the repeated date with their respective records.

What I'm trying to achieve is something like this:

enter image description here

How could I do it?

PD: The result that I'm getting is this:

enter image description here

enter image description here

enter image description here

enter image description here

But I don't like that...

  • 写回答

1条回答 默认 最新

  • douzen1896 2018-06-02 16:02
    关注

    The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns. If there are records in the "Orders" table that do not have matches in "Customers", these orders will not be shown!

    The following query should no longer generate duplicate records

    SELECT 
        ar.id,
        ar.task,
        date_format(ar.date, '%d/%m/%Y') as formattedDate, 
        date_format(ar.time, '%r') as formattedTime, 
        u.user as User 
    FROM 
        task_recents ar 
    LEFT JOIN 
        user u 
    ON 
        u.iduser = ar.iduser 
    WHERE 
        date = '$date' 
    ORDER BY 
        ar.time 
    DESC
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧