Php 混合来自两个不同行的值

Php 混合来自两个不同行的值,php,mysql,Php,Mysql,您好,我的数据库中有一个产品折扣表,如下所示: id product quantity NewPrice 目前在我的php网站上: 每行: 购买$quanity或以上时的单价:$NewPrice 但我想说的是: 购买$quanity1-$quanity2时的单价:$newprice 1 购买$quanity2-$quanity3时的单价:$newprice 2 购买$quantity3或更多时的单价:$NewPrice3 所以基本上我想从两行中取Quantity值来组成第一行中价格的文本 如

您好,我的数据库中有一个产品折扣表,如下所示:

id  product quantity NewPrice
目前在我的php网站上:

每行:
购买$quanity或以上时的单价:$NewPrice

但我想说的是:

购买$quanity1-$quanity2时的单价:$newprice 1
购买$quanity2-$quanity3时的单价:$newprice 2
购买$quantity3或更多时的单价:$NewPrice3

所以基本上我想从两行中取Quantity值来组成第一行中价格的文本


如果有意义的话?

只需通过
[product column]
[quantity column]
排序从
[折扣表]
获取所有数据即可

然后提取
[quantity column]
值并使用数组

$quantities=数组(quantity0、quantity1、quantity2)
//$数量[0]$数量[n]

  • 从【折扣表】中选择,其中【产品列】=“someid”订单由【数量列】ASC提供
  • $quantities=array()
  • foreach($r行){$quantities[]=$r[
    数量列名
    ];}
您将得到数量数组,从最小到最大排序

确切的过程取决于您的数据库层、php框架等。

只需通过
[product column]
[quantity column]
排序从
[折扣表]
获取所有数据即可

然后提取
[quantity column]
值并使用数组

$quantities=数组(quantity0、quantity1、quantity2)
//$数量[0]$数量[n]

  • 从【折扣表】中选择,其中【产品列】=“someid”订单由【数量列】ASC提供
  • $quantities=array()
  • foreach($r行){$quantities[]=$r[
    数量列名
    ];}
您将得到数量数组,从最小到最大排序

具体过程取决于您的数据库层、php框架等。

按产品、数量对结果进行排序,然后循环结果并与前一行进行比较:

$lastRow = null;
$sql = "SELECT * FROM prices ORDER BY product, quantity";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    if ($lastRow and $lastRow['product'] == $row['product']) {
        echo "Price per unit when buying ".$lastRow['quantity']." - ".($row['quantity']-1).": ".$lastRow['price']."\n<br>";
    } elseif ($lastRow) {
        echo "Price per unit when buying ".$lastRow['quantity']." or more: ".$lastRow['price']."\n<br>";
    }
    $lastRow = $row;
}
if ($lastRow) {
    echo "Price per unit when buying ".$lastRow['quantity']." or more: ".$lastRow['price']."\n<br>";
}
$lastRow=null;
$sql=“按产品、数量从价格订单中选择*”;
$result=mysql\u查询($sql);
while($row=mysql\u fetch\u数组($result)){
如果($lastRow和$lastRow['product']==$row['product'])){
回显“购买时单位价格”。$lastRow['quantity'].“-”($row['quantity']-1)。:“$lastRow['Price'.”\n
”; }elseif($lastRow){ 回显“购买时每单位的价格”。$lastRow['quantity']”或更多:“$lastRow['Price']”。\n
”; } $lastRow=$row; } 如果($lastRow){ 回显“购买时每单位的价格”。$lastRow['quantity']”或更多:“$lastRow['Price']”。\n
”; }
按产品、数量对结果进行排序,然后循环结果并与前一行进行比较:

$lastRow = null;
$sql = "SELECT * FROM prices ORDER BY product, quantity";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    if ($lastRow and $lastRow['product'] == $row['product']) {
        echo "Price per unit when buying ".$lastRow['quantity']." - ".($row['quantity']-1).": ".$lastRow['price']."\n<br>";
    } elseif ($lastRow) {
        echo "Price per unit when buying ".$lastRow['quantity']." or more: ".$lastRow['price']."\n<br>";
    }
    $lastRow = $row;
}
if ($lastRow) {
    echo "Price per unit when buying ".$lastRow['quantity']." or more: ".$lastRow['price']."\n<br>";
}
$lastRow=null;
$sql=“按产品、数量从价格订单中选择*”;
$result=mysql\u查询($sql);
while($row=mysql\u fetch\u数组($result)){
如果($lastRow和$lastRow['product']==$row['product'])){
回显“购买时单位价格”。$lastRow['quantity'].“-”($row['quantity']-1)。:“$lastRow['Price'.”\n
”; }elseif($lastRow){ 回显“购买时每单位的价格”。$lastRow['quantity']”或更多:“$lastRow['Price']”。\n
”; } $lastRow=$row; } 如果($lastRow){ 回显“购买时每单位的价格”。$lastRow['quantity']”或更多:“$lastRow['Price']”。\n
”; }