2
Product |  bprice  |    fprice
abcd    |  89      |    65
ebcd    |  39      |    105
fbcd    |  23      |    45
gbcd    |  89      |    
hbcd    |  89      |    65
ibcd    |          |    65
jbcd    |  50      |    50

sql+php

获取mysql记录后如何使用php脚本获得每种产品的低价

abcd $price = 65
fbed $price = 23
gbcd $price = 89

4

3 回答 3

5

min($bprice,$fprice)应该做你需要的。


编辑:修复处理空

if (is_null($bprice)) {
    $price = $fprice;
} elseif (is_null($fprice)) {
    $price = $bprice;
} else {
    $price = min($bprice,$fprice);
}
于 2012-04-16T07:16:24.390 回答
3

使用 group by 语句:

SELECT MAX(bprice) AS maxbprice, MAX(fprice) AS maxfprice
FROM products
GROUP BY Product
于 2012-04-16T07:16:39.860 回答
1
$query = "SELECT * FROM product group by product";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_object($result))
{
   if($row->bprice > $row->fprice)
   {
       $low=$row->fprice;
   } 
    else
   {
   $low=$row->bprice;
    }
}
于 2012-04-16T07:31:45.873 回答