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

Time as a category list using GROUP BY - MYSQL

我想使用 time 作为类别并且我有以下表格:

Table addonlist_final

Table 插件

我希望输出如下所示:

Order at 11:17 am

Coke 1 Litre

Ham & Cheese Sandwich

Order at 11:15 am

Coke 1 Litre

Hotdog Sandwich

Tuna Sandwich

我目前所做的是这段代码,但只显示组中的第一个item

<?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 />";
}

?>

输出:

Ordered at 11:15 am

Coke 1 Litre

Ordered at 11:17 am

Coke 1 Litre

使用ORDER BY

您的SELECT声明

SELECT ddonlist_final.faddon_id,addonlist_final.timef , addons.derscription
FROM addonlist_final
INNER JOIN addons
ON addonlist_final.faddon_id=addons.addon_id
ORDER BY addonlist_final.timef ASC LIMIT 10;

然后在 PHP

$new_array = array();
while($rows = mysql_fetch_assoc($getaddons)){

  if($rows['faddon_id']){
      $new_array['timef'][] = $new_array['derscription'];
  }

}


   foreach($new_array as $time=>$addons){
       foreach($addons as $addon)
        echo "Ordered at $time<br />";
        echo "$addon<br />";
    }

尝试以下方法

<?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 />";
}

尝试将您的查询更改为:

SELECT a.faddonid, c.description, a.quantity, c.price, a.timef FROM addonlist_final a
INNER JOIN (SELECT timef FROM addonlist_final 
  WHERE  ftransac_id = '96675' GROUP BY timef) b
  ON a.timef = b.timef
INNER JOIN addons c
ON a.faddon_id = c.addon_id
WHERE a.ftransac_id = '96675'