我有一個 Orders 項目,我想在其中實現標簽打印。每個訂單必須根據其物品數量顯示標簽數量。到目前為止,我已經設法用“for”來顯示數量。數據庫示例:+-------+-------------+-----+| order | description | qty |+-------+-------------+-----+| 1 | stickers | 1 || 2 | posters | 2 || 2 | tape | 1 || 2 | (no desc) | 1 |+-------+-------------+-----+到目前為止我所擁有的:Select SUM(qty) AS pieces FROM mytable WHERE order = '2'"); $total=$row['pieces']; //this correspond to all items on the order.for ($x = 1; $x <= $row_count; $x++){ echo $order."<br>"; //order number echo $x."<br>"; //item echo $total."<br>"; //total items}結果: #order #item #total #order #item #total +-------+-------+-------+ +-------+-------+-------+posters | 2 | 1 | 4 | | 2 | 2 | 4 | +-------+--- ---+-------+ +-------+-------+-------+ #order #item #total +-------+-------+-------+tape | 2 | 3 | 4 | +-------+-------+-------+ #order #item #total +-------+-------+-------+(no desc) | 2 | 4 | 4 | +-------+-------+-------+但是我無法顯示每個“描述”欄的內容。理想的結果: #order #desc #item #total #order #desc #item #total +---+--------+----+----+ +---+--------+----+----+posters | 2 | posters| 1 | 4 | | 2 | posters| 2 | 4 | +---+--------+----+----+ +---+--------+----+----+ #order #desc #item #total +---+--------+----+----+tape | 2 | tape | 3 | 4 | +---+--------+----+----+ #order #desc #item #total +---+---------+----+----+(no desc) | 2 |(no desc)| 4 | 4 | +---+---------+----+----+有機會得到這個結果嗎?我正在使用 php 和 mysql。
1 回答

慕妹3146593
TA貢獻1820條經驗 獲得超9個贊
您可以將總查詢與表本身的查詢結合起來。
SELECT m.description, m.qty, t.total
FROM mytable AS m
CROSS JOIN (
SELECT SUM(qty) AS total
FROM mytable
WHERE order = 2
) AS t
WHERE order = 2
然后你可以使用嵌套循環:
$j = 1;
while ($row = $stmt->fetch()) {
echo $row['description'];
for ($i = 1; $i <= $row['qty']; $i++, $j++) {
echo "$order<br>{$row['description']}<br>$j<br>{$row['total']}<br>";
}
}
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報
0/150
提交
取消