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
等
答案 0 :(得分:5)
min($bprice,$fprice)
应该做你需要的。
if (is_null($bprice)) {
$price = $fprice;
} elseif (is_null($fprice)) {
$price = $bprice;
} else {
$price = min($bprice,$fprice);
}
答案 1 :(得分:3)
使用group by语句:
SELECT MAX(bprice) AS maxbprice, MAX(fprice) AS maxfprice
FROM products
GROUP BY Product
答案 2 :(得分: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;
}
}