【发布时间】:2012-03-21 08:17:26
【问题描述】:
例如,我有一张桌子:
id name type price
123451 Park's Great Hits Music 19.99
123452 Silly Puddy Toy 3.99
123453 Playstation Toy 89.95
123454 Men's T-Shirt Clothing 32.50
123455 Blouse Clothing 34.97
123456 Electronica 2002 Music 3.99
123457 Country Tunes Music 21.55
123458 Watermelon Food 8.73
如果我想按类型选择最便宜的价格,我使用这个:
<?php
// Make a MySQL Connection
$query = "SELECT type, MIN(price) FROM products GROUP BY type";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "The cheapest ". $row['type']. " is $" .$row['MIN(price)'];
echo "<br />";
}
?>
然后会显示:
The cheapest Clothing is $32.50
The cheapest Food is $8.73
The cheapest Music is $3.99
The cheapest Toy is $3.99
但是,我想知道最便宜商品的详细信息,以便显示:
The cheapest Clothing is $32.50 which is Men's T-Shirt and id is 123454
The cheapest Food is $8.73 which is Watermelon and id is 123458
The cheapest Music is $3.99 which is Electronica 2002 and id is 123452
The cheapest Toy is $3.99 which is SillyPuddy and id is 123452
请帮忙!!
【问题讨论】:
-
同类型的两件商品价格最低时应该怎么做?两者都会显示吗?
-
我假设有一列是输入时间,如果它们是相同的最便宜的价格,则显示最新的
-
@red23jordan 这个时间值会在单独的表中吗?
标签: mysql