2012-09-23 175 views
0

我有這樣的MySQL代碼女巫工作得很好的MySQL連接兩個表MAX(值)

SELECT uid , MAX(value) AS bidvalue FROM auction 
WHERE pid = '$pid' and max_bid=0 
GROUP BY uid 
ORDER BY bidvalue DESC 
LIMIT 1 

我怎樣才能將這個選擇與用戶表ON users.id = auction.uid?

謝謝

回答

1

您可以使用加入爲你的查詢可能看起來像

SELECT uid, users.name, MAX(value) AS bidvalue 
FROM auction 
INNER JOIN users ON users.id = auction.uid 
WHERE pid = '$pid' and max_bid=0 
GROUP BY uid 
ORDER BY bidvalue DESC 
LIMIT 1 

SELECT uid, users.name, MAX(value) AS bidvalue 
FROM auction 
OUTER JOIN users ON users.id = auction.uid 
WHERE pid = '$pid' and max_bid=0 
GROUP BY uid 
ORDER BY bidvalue DESC 
LIMIT 1 

with the joins details as per your need

+0

就是這樣。非常感謝你。 – Coscho

1

我在這裏看不到任何魔法:

SELECT uid, users.name, MAX(value) AS bidvalue 
FROM auction 
INNER JOIN users ON users.id = auction.uid 
WHERE pid = '$pid' and max_bid=0 
GROUP BY uid 
ORDER BY bidvalue DESC 
LIMIT 1