2014-04-09 50 views
0

與條件的兩個表我想使這兩個成一個SELECT語句:加入第一臺

SELECT * 
FROM activegroupmodel 
WHERE groupID = ? 
ORDER BY groupModelID DESC 
LIMIT 1 

SELECT * 
FROM model 
WHERE modelID = ? 

你可以從第一組ID所看到的不是一個PK,我想從最新型號表。 心中已經試過,但它顯然沒有奏效:

SELECT * 
FROM activegroupmodel as a 
WHERE a.groupID = ? 
ORDER BY a.groupModelID DESC 
LIMIT 1 
INNER JOIN model as m ON a.modelID = m.modelID 

我讀了一堆帶着疑問加入,但他們沒有我的問題相匹配。有人知道我能如何解決這個問題嗎? 最好的問候約翰

+0

也許你應該把JOIN放在WHERE – marcosh

+0

之前就是這麼簡單......謝謝@marcosh – Mangs

回答

1

只是把WHERE條件之前你的加盟,這樣的:

SELECT * 
FROM activegroupmodel as a 
INNER JOIN model as m ON a.modelID = m.modelID 
WHERE a.groupID = ? 
ORDER BY a.groupModelID DESC 
LIMIT 1 
0
SELECT * 
FROM activegroupmodel as a 
INNER JOIN model as m ON a.modelID = m.modelID 
WHERE a.groupID = ? 
ORDER BY a.groupModelID DESC 
LIMIT 1 
0

正確連接語句應儘可能

SELECT * 
FROM activegroupmodel as a 
INNER JOIN model as m ON a.modelID = m.modelID 
WHERE a.groupID = ? 
ORDER BY a.groupModelID DESC 
LIMIT 1 
0

試試這個

SELECT * 
FROM activegroupmodel as a 
INNER JOIN model as m ON a.modelID = m.modelID 
WHERE a.groupID = ? 
ORDER BY a.groupModelID DESC 
LIMIT 1