2017-09-26 201 views
2

我有一個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級是我使用)同時不限制整個查詢的結果數量。

通過運行上面的查詢,我得到我期望的數據,但只有一條記錄,當它應該返回幾個。

我的問題:

如何修改這個查詢,以確保我收到的所有可能的記錄可用,同時確保僅從報告的最新記錄連接使用?我希望每一個記錄將可能包含不同的數據從另一個連接,但此查詢返回的所有記錄將共享相同的報告記錄

+0

你使用什麼? MySQL或SQL Server? – waka

+0

MySQL。我會更新問題。 –

+0

@waka更新.. –

回答

2

提供我明白這個問題;可以將一個連接添加到一組數據(下面的別名Z),該連接對每個學生都有最大時間戳;從而限制每個學生的一個報告記錄(最新)。

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 
join (SELECT max(timestamp) mts, studentID 
     FROM REPORTS 
     GROUP BY StudentID) Z 
    on reports.studentID = Z.studentID 
and reports.timestamp = Z.mts 
WHERE userstudentrelationship.studentID = "10000005" 
    AND userAccounts.getTexts = 1 
ORDER BY reports.timestamp 
+0

這個答案完美解決了它。謝謝!我想我是在解釋情況下讓一些人感到困惑,但我不想要一個TLDR的時刻。聯繫結果永遠不會改變,因爲報表結果中只有一個可能的聯繫人。唯一可以真正改變的是userAccounts。 –

+0

如果您之後所有的都是max timestmap,我們可以按其他字段進行分組並使用聚合;但是因爲你還需要報告中的pupID;我們需要關於每個學生的特定最大報告記錄的額外信息。 – xQbert

1

爲得到所有的記錄,你應該避免限制1在查詢結束
從報告表加入一行你可以使用子查詢作爲

select 
    students.studentID 
    , students.firstName 
    , students.lastName 
    , userAccounts.userID 
    , userstudentrelationship.userID 
    , userstudentrelationship.studentID 
    , userAccounts.getTexts 
    , t.pupID 
    , contacts.pfirstName 
    , contacts.plastName 
    , t.timestamp 

from userstudentrelationship 

join userAccounts on userstudentrelationship.userID = userAccounts.userID 
join students on userstudentrelationship.studentID = students.studentID 
join (
    select * from reports 
    order by reports.timestamp limit 1 
) t on students.studentID = t.studentID 
join contacts on reports.pupID = contacts.pupID 

where userstudentrelationship.studentID = "10000005" 
AND userAccounts.getTexts = 1 
+0

這將給每個學生1個單獨的記錄。我想他希望得到每個學生的最新報告 –