質問

I want to make these two into one SELECT statement:

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

SELECT * 
FROM model 
WHERE modelID = ?

As you can see from the first groupID is not a pk and I want the latest model from that table. I'v tried this but it obviously didn't work:

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

I've read a bunch of questions with JOIN but none of them matches my problem. Anybody knows how I can solve this? Best regards Johan

役に立ちましたか?

解決

just put your JOIN before the WHERE condition, like this:

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 

他のヒント

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 

The correct join statement should be as

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 

Try this

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 
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top