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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)