我的数据库是 http://i.imgur.com/p4wUP54.jpg 嘿,我是新来的PHP,我不知道如何为产品拍卖找到最低的独特出价者,我还想显示该最低独特出价者的用户名。 请帮助我,我已经尝试了一些代码,但是
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gunjanbid", $con);
$sql="SELECT * from bid";
$result=mysql_query($sql);
$username=$result['userName'];
$serial=$result['serial'];
$low_sql = "SELECT bidamount,userName, COUNT(bidamount) as bid_count
FROM bid
WHERE bidamount<'{$serial}' AND bid_count=1 AND auction='{$username}'
GROUP BY bidamount";
$low_query = mysql_query($low_sql) ;
$low_bids = array();
if (mysql_num_rows($low_query) > 0)
{
$lower_bid_exist = true;
while ($row = mysql_fetch_assoc($low_query))
{
$low_bids[$row['bidamount']] = $row['bid_count'];
}
} else {
$lower_bid_exist = false;
}
?>
Lowest Unique Bidder :<?php echo $row['userName']; ?>
答案 0 :(得分:0)
试试这个:
SELECT bidamount,userName, bidamount as bidamount
FROM bid WHERE bidamount = (SELECT MIN(bidamount) FROM bid) ORDER BY closing_date LIMIT 1
答案 1 :(得分:0)
行。因此,首先,您需要通过一些计划来解决这个问题。
您的要求是: - 出价金额必须是唯一的 - 您想选择最低出价金额
有很多方法可以做到这一点。我将向您展示在单个查询中使用子查询来解决问题的最简单(SQL明智)方法。
这不是最好的方法 - 正如我通常会建议两个单独的查询一样,但这样可以在单行返回中获得您想要的内容。
http://sqlfiddle.com/#!2/2e978/10
SQL成为:
select
bidamount,
username
from
bid
where
-- for the auction in question
auction = '$username'
and bidamount = (
select
min(lowest_bid.bidamount)
from
bid lowest_bid
where
lowest_bid.auction = '$username'
group by
lowest_bid.bidamount
having
count(distinct lowest_bid.username) = 1
order by
bidamount
limit 1);