Domanda

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

È stato utile?

Soluzione

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 

Altri suggerimenti

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 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top