Reputation: 19
Product | bprice | fprice
abcd | 89 | 65
ebcd | 39 | 105
fbcd | 23 | 45
gbcd | 89 |
hbcd | 89 | 65
ibcd | | 65
jbcd | 50 | 50
sql+php
how to get low price of each product using php script after fetching mysql records like
for abcd $price = 65
for fbed $price = 23
for gbcd $price = 89
etc
Upvotes: 2
Views: 375
Reputation: 5668
min($bprice,$fprice)
should do what you need.
if (is_null($bprice)) {
$price = $fprice;
} elseif (is_null($fprice)) {
$price = $bprice;
} else {
$price = min($bprice,$fprice);
}
Upvotes: 5
Reputation: 7302
Use the group by statement:
SELECT MAX(bprice) AS maxbprice, MAX(fprice) AS maxfprice
FROM products
GROUP BY Product
Upvotes: 3
Reputation: 2363
$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;
}
}
Upvotes: 1