我是PHP&的新手MySQL的。我通过几个论坛搜索,但不知道如何让这个QUERY工作。希望你们能指导我。
ITEMINFO
--------------------------------------
| ItemNo | Description | Price |
| AAAAA | AAAAA description | $1.11 |
| BBBBB | BBBBB description | $2.22 |
| CCCCC | CCCCC description | $3.33 |
STOREINFO
-----------------------------------------------------------
| StoreNo | Add1 | Add2 | City | State | Zip |
| 11111 | 111 Main St | # 1 | New York | NY | 10001 |
| 22222 | 222 Main St | # 2 | New York | NY | 10001 |
| 33333 | 333 Main St | # 3 | New York | NY | 10001 |
| 44444 | 444 Main St | # 4 | New York | NY | 10001 |
STOREORDER
-------------------------------
| StoreNo | Quantity | ItemNo |
| 11111 | 10 | AAAAA |
| 11111 | 5 | BBBBB |
| 33333 | 50 | BBBBB |
| 22222 | 20 | AAAAA |
QUERY:列出已订购的所有商店;以及哪个项目&订购了数量?
$query = "SELECT * FROM STOREINFO ".
"LEFT JOIN STOREORDER ON STOREINFO.StoreNo = STOREORDER.StoreNo ".
"LEFT JOIN ITEMINFO ON STOREORDER.ItemNo = ITEMINFO.ItemNo ".
"ORDER BY STOREINFO.StoreNo ASC";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
$CheckStoreNo = trim($row['StoreNo']);
if($CheckStoreNo != "")
{
echo " *print store info* ";
---->>>> echo " *list ALL item ordered & qty* ";
}
}
echo "<hr>";
?>
输出:(已格式化)
Company: Store # 11111
111 Main St, # 1, New York, NY 10001
Tel: 111-111-1111
This store ordered - Item: AAAAA - 10 pc(s)
Company: Store # 11111
111 Main St, # 1, New York, NY 10001
Tel: 111-111-1111
This store ordered - Item: BBBBB - 5 pc(s)
Company: Store # 22222
222 Main St, # 2, New York, NY 10001
Tel: 222-222-2222
This store ordered - Item: AAAAA - 20 pc(s)
Company: Store # 33333
333 Main St, # 3, New York, NY 10001
Tel: 333-333-3333
This store ordered - Item: BBBBB - 50 pc(s)
我设法做到这一点。现在,如果您注意到商店#11111实际上有两(2)个不同商品的订单。而不是ECHO所有重复的公司信息,我只需要一次SAME公司信息ECHO并列出为商店#11111订购的所有商品。
示例:这就是我需要的东西!
Company: Store # 11111
111 Main St, # 1, New York, NY 10001
Tel: 111-111-1111
1) Item: AAAAA - 10 pc(s)
2) Item: BBBBB - 5 pc(s)
Company: Store # 22222
... And so on ...
我根据每家商店的订单列出了所有订单。 (感谢@cularis)。
现在,我如何在每个商店的订单后关闭/结束它?
示例:关闭每个商店的打印目的订单!
Company: Store # 11111
111 Main St, # 1, New York, NY 10001
Tel: 111-111-1111
1) Item: AAAAA - 10 pc(s)
2) Item: BBBBB - 5 pc(s)
------- End of order for Store # 11111 -----
Company: Store # 22222
... List order ...
------- End of order for Store # 22222 -----
答案 0 :(得分:1)
这不是MySQL,而是PHP问题。
你必须在你的前端这样做:
// Print out the contents of each row into a table
$OldStoreNo = ""
while($row = mysql_fetch_array($result)){
$CheckStoreNo = trim($row['StoreNo']);
if($CheckStoreNo != "") {
if($OldStoreNo != $CheckStoreNo) {
echo " *print store info* ";
$OldStoreNo = $CheckStoreNo;
}
echo " *list item ordered & qty* ";
}
}
与OldStoreNo一起,您可以跟踪每个商店的当前商品数量,以便在每个商品旁边输出一个数字。
答案 1 :(得分:0)
跟踪while循环中的商店编号 如果它与前一个相同,则打印订单,否则打印storeinfo和订单
//track store_no
$current_store_no = false;
while($row = mysql_fetch_array($result)){
if (trim($row['StoreNo']) != $current_store_no) {
//print store info
$current_store_no = trim($row['StoreNo'])
}
//print order info
}