![](/img/trans.png)
[英]how to join two tables where the condition 'Where' is related to a third table that is associated with the first table
[英]Join two tables with condition on the first table
我想將這兩個變成一個SELECT語句:
SELECT *
FROM activegroupmodel
WHERE groupID = ?
ORDER BY groupModelID DESC
LIMIT 1
SELECT *
FROM model
WHERE modelID = ?
正如您從第一個groupID看到的不是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
我已經用JOIN讀了很多問題,但是沒有一個與我的問題相符。 有人知道我該如何解決? 最好的問候約翰
只需將JOIN放在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
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
嘗試這個
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 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.