doutang7414 2015-01-24 04:16 采纳率: 0%
浏览 90
已采纳

使用GROUP BY - MYSQL作为类别列表的时间

I want to use time as a category and I have this tables:

Table addonlist_final

enter image description here

Table addons

enter image description here

I want the output to be look like this:

Order at 11:17 am

Coke 1 Litre

Ham & Cheese Sandwich

Order at 11:15 am

Coke 1 Litre

Hotdog Sandwich

Tuna Sandwich

What I've currently done is this code, but only shows the first item on the group.

<?php

$getaddons = mysql_query("SELECT a.addon_id, a.description, a.price, a.status, fl.faddon_id, fl.quantity, fl.timef
                          FROM addons AS a
                          LEFT JOIN addonlist_final AS fl ON a.addon_id = fl.faddon_id AND fl.ftransac_id = '96675'
                          GROUP BY fl.timef
                          LIMIT 10
                          ");
while($rows = mysql_fetch_assoc($getaddons)){
    $addonid = $rows['addon_id'];
    $addondesc = $rows['description'];
    $addonprice = $rows['price'];
    $addonstat = $rows['status'];
    $quantity = $rows['quantity'];
    $timef = $rows['timef'];
    $total = $addonprice * $quantity;

    if($rows['faddon_id']){
        echo "Ordered at $timef<br />";
        echo "$addondesc<br />";
    }
    echo "<br />";
}

?>

Output:

Ordered at 11:15 am

Coke 1 Litre

Ordered at 11:17 am

Coke 1 Litre

  • 写回答

3条回答 默认 最新

  • dqw7121 2015-01-24 04:46
    关注

    Try the following

    <?php
    
    include("conn.php");
    
    date_default_timezone_set('Asia/Manila');
    $time = date('h:i:s a', time());
    $date = (date("D F d Y"));
    
    echo $date." ".$time."<br /><br /><br />";
    
    $getaddons = mysql_query("SELECT a.*, af.timef, af.quantity FROM addons AS a, addonlist_final AS af WHERE a.addon_id = af.faddon_id ORDER BY af.timef DESC");
    
    
    $list = array();
    
    while($rows = mysql_fetch_assoc($getaddons)){
        $timef = $rows['timef'];
        $list[$timef][] = $rows; 
    }
    
    foreach($list as $time => $group) {
      echo "Ordered at $time<br />";
      foreach($group as $rows) {
        $addonid = $rows['addon_id'];
        $addondesc = $rows['description'];
        $addonprice = $rows['price'];
        $addonstat = $rows['status'];
        $quantity = $rows['quantity'];
        $timef = $rows['timef'];
        $total = $addonprice * $quantity;
    
        if($rows['faddon_id']){
            echo "$addondesc<br />";
        }
      }
      echo "<br />";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效