Php 将当前记录与上一记录进行比较
我是PHP和MySQL新手。我用谷歌搜索了几个论坛,但不知道如何让这个查询起作用。希望你们能指引我。Php 将当前记录与上一记录进行比较,php,mysql,Php,Mysql,我是PHP和MySQL新手。我用谷歌搜索了几个论坛,但不知道如何让这个查询起作用。希望你们能指引我。 ITEMINFO -------------------------------------- | ItemNo | Description | Price | | AAAAA | AAAAA description | $1.11 | | BBBBB | BBBBB description | $2.22 | | CCCCC | CCCCC description | $3.
ITEMINFO
--------------------------------------
| ItemNo | Description | Price |
| AAAAA | AAAAA description | $1.11 |
| BBBBB | BBBBB description | $2.22 |
| CCCCC | CCCCC description | $3.33 |
-----------------------------------------------------------
| 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 |
-------------------------------
| StoreNo | Quantity | ItemNo |
| 11111 | 10 | AAAAA |
| 11111 | 5 | BBBBB |
| 33333 | 50 | BBBBB |
| 22222 | 20 | AAAAA |
STOREINFO
--------------------------------------
| ItemNo | Description | Price |
| AAAAA | AAAAA description | $1.11 |
| BBBBB | BBBBB description | $2.22 |
| CCCCC | CCCCC description | $3.33 |
-----------------------------------------------------------
| 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 |
-------------------------------
| StoreNo | Quantity | ItemNo |
| 11111 | 10 | AAAAA |
| 11111 | 5 | BBBBB |
| 33333 | 50 | BBBBB |
| 22222 | 20 | AAAAA |
STOREORDER
--------------------------------------
| ItemNo | Description | Price |
| AAAAA | AAAAA description | $1.11 |
| BBBBB | BBBBB description | $2.22 |
| CCCCC | CCCCC description | $3.33 |
-----------------------------------------------------------
| 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 |
-------------------------------
| StoreNo | Quantity | ItemNo |
| 11111 | 10 | AAAAA |
| 11111 | 5 | BBBBB |
| 33333 | 50 | BBBBB |
| 22222 | 20 | AAAAA |
查询:列出所有已订购的门店;订购了哪些项目和数量?$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>";
?>
我设法做到了这一点。现在,如果你注意到商店#11111实际上有两(两)份不同商品的订单。我不需要回显所有重复的公司信息,只需要回显一次相同的公司信息,并列出为商店#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 ...
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 -----
我根据每家商店的订单列出了所有订单。(感谢@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)
Company: Store # 22222
... And so on ...
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 -----
这不是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一起,您可以跟踪每个门店的当前商品数量,以便在每个商品旁边输出一个数字。在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
}
一切都很好!多谢各位=D下一个问题:在列出每个商店的所有商品(用于向每个商店打印)后,如何关闭它?在打印下一个商店的店铺信息之前,请打印上一个商店之后的所有信息。您必须检查它是否是第一个带有布尔值的存储,因为这样您就不想打印店铺结束信息。如果($CurrentStoneNo!=$CheckStoreNo和$numcount>0){print-end-ORDER},我会在“$CheckStoreNo=trim($row['StoreNo']);”之后插入if语句。我不认为这是正确的方法(它无法关闭“上次订单”。请建议=(您必须在while循环结束后最后打印一次门店信息)。