我正在尝试使用 PHP 显示按订单号分组的单个客户订单的详细信息。我可以通过运行单独的查询来完成此基本操作,但这似乎效率不高。有人可以帮助我以最有效的方式做到这一点吗?请参阅下面的示例:
我有一个“orders”MySQL 表,其结构如下:
|ordernumber|customer|quantity|sku|product |orderdate
-----------------------------------------------------
|1 |bob |2 |aaa|producta|2012-08-30
|1 |bob |1 |bbb|productb|2012-08-30
|1 |bob |4 |ccc|productc|2012-08-30
|2 |jim |10 |aaa|producta|2012-08-30
|2 |jim |1 |ccc|productc|2012-08-30
|3 |bob |1 |bbb|productb|2012-12-15
|3 |bob |4 |ccc|productc|2012-12-15
我想以这种格式输出鲍勃的订单:
Customer: Bob
Order ID: 1
Order Date: 2012-08-30
Details: *Qty* *Sku* *Product*
2 aaa producta
1 bbb productb
4 ccc productc
____________________________________________________
Customer: Bob
Order ID: 3
Order Date: 2012-12-15
Details: *Qty* *Sku* *Product*
1 bbb productb
4 ccc productc
最佳答案
我将运行一个查询 - 选择所有客户为 bob 的位置并按订单号订购。
然后我只需迭代结果并打印 sku。
每当我在迭代中看到新的订单号时,我都会打印格式以启动新订单(并回显其所有元数据,如日期、订单号等)
这是伪代码:
rows = run_sql(select * where customer = bob order by ordernumber asc)
curr_order_id = 0
foreach (row in rows)
{
if (row[order_id] != curr_order_id)
{
curr_order_id = row[order_id]
print(-----------------------)
print(customer : row[customer])
print(order : row[order_id])
print(order date: row[order_date])
print(details: *qty* *sku* *product*)
}
print( row[qty] row[sku] row[product])
}
关于php - 循环遍历 MySQL 结果循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14697360/