I have the follwing structure:
id,name,product,price
Now, I want to know how I can get the lowest value from price - and - get the name that belongs to the price. Here's a example:
0,seller1,cake,5
1,seller2,cake,2.50
Obviously seller2 has the lowest price. But I need to get that price - and the name that belongs to that price - and display it in PHP. Something like this:
echo $seller . " sells " . $product . " for " . $price . ".";
I hope I have been clear enough.
Kind regards,
Hillebrand
maybe
$table = mysql_query("SELECT name, price FROM table ORDER BY price ASC LIMIT 1");
while($y = mysql_fetch_object($table))
{
echo $y->name;
}
The SQL
to select what you need would be:
SELECT name, product, price FROM `table` ORDER BY price LIMIT 1
(Note that you didn't provide the table name so you'll need to replace table
with the correct name.)
You can then use mysqli_stmt_fetch
to fetch the results:
$stmt = $mysqli->prepare("SELECT name, product, price FROM `table` ORDER BY price LIMIT 1");
$stmt->execute();
$stmt->bind_result($seller, $product, $price);
$stmt->fetch();
echo $seller . ' sells ' . $product . ' for ' . $price . '.';
Keep in mind that this will only select the first product with the lowest price. You may need to consider how you'd like this to behave if you have two or more items which are equal in having the lowest price (eg 0
). Should it display them all? Should there be some other field to signify precedence?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.