MySQL通过子查询按组查询

时间:2017-09-20 07:30:56

标签: php mysql

$stmt = $conn->prepare("SELECT bid.bidID, bid.bidJobID, 
bid.bidVanManID, bid.bidMemberID, bid.bidAmount, 
van.vanmanCompanyName, van.vanmanUsername,
job.jobID, job.acceptedQuote FROM bids bid 
JOIN vanman van 
ON 
bidVanManID = van.vanmanID 
JOIN jobs job 
ON
bidJobID = job.jobID WHERE bidJobID = ? AND bidMemberID = ?");

目前,它从出价表中选择所有出价并加入vanman表以检索每个出价的vanman详细信息。

正在返回重复的vanman(这很好,因为我允许每个vanman最多3个出价,所以这是预期的)

但是,我想从同一个vanman中删除重复的出价,只留下最近的出价(bidTime)

我在JOIN vanman上尝试了一个子查询来选择distinct,但是,我的MySQL知识仅限于某些基本查询。

任何帮助非常感谢。感谢

$stmt = $conn->prepare("SELECT bid.bidID, MAX(bid.bidTime), bid.bidJobID, 
bid.bidVanManID, bid.bidMemberID, bid.bidAmount, 
van.vanmanCompanyName, van.vanmanUsername,
job.jobID, job.acceptedQuote FROM bids bid 
JOIN vanman van 
ON 
bidVanManID = van.vanmanID 
JOIN jobs job 
ON
bidJobID = job.jobID WHERE bidJobID = ? AND bidMemberID = ?
GROUP BY vanmanID ORDER BY bidTime DESC");

上述工作现在删除重复项,就像我之前所做的那样,但它不会留下具有最​​新bidTime的那个

0 个答案:

没有答案