문제

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