我有一個MYSQL查詢我正在從多個連接拉數據。MySQL多個連接查詢限制在一個連接
select students.studentID, students.firstName, students.lastName, userAccounts.userID, userstudentrelationship.userID, userstudentrelationship.studentID, userAccounts.getTexts, reports.pupID, contacts.pfirstName, contacts.plastName, reports.timestamp
from userstudentrelationship
join userAccounts on (userstudentrelationship.userID = userAccounts.userID)
join students on (userstudentrelationship.studentID = students.studentID)
join reports on (students.studentID = reports.studentID)
join contacts on (reports.pupID = contacts.pupID)
where userstudentrelationship.studentID = "10000005" AND userAccounts.getTexts = 1 ORDER BY reports.timestamp DESC LIMIT 1
我有一個獨特的情況,我想的加入一個的報告(加入)僅限於最新的結果只對表(由reports.timestamp遞減限制1級是我使用)同時不限制整個查詢的結果數量。
通過運行上面的查詢,我得到我期望的數據,但只有一條記錄,當它應該返回幾個。
我的問題:
如何修改這個查詢,以確保我收到的所有可能的記錄可用,同時確保僅從報告的最新記錄連接使用?我希望每一個記錄將可能包含不同的數據從另一個連接,但此查詢返回的所有記錄將共享相同的報告記錄
你使用什麼? MySQL或SQL Server? – waka
MySQL。我會更新問題。 –
@waka更新.. –