我有这三张桌子
people
============
id, name
,
surveys
============
id, org_id
和
answer_sheets
============
id, person_id, survey_id, answer, date_answered
person_id
and是来自andsurvey_id
的外键people.id
surveys.id
现在,我想做的是people
根据最新的排序answer_sheets.date_answered
(我们可以得出一个surveys
ANDpeople
行可以有很多answer_sheets
)给定surveys.org_id
比如说我们有桌子
people
============
id name
1 Person1
2 Person2
3 Person3
4 Person4
5 Person5
surveys
============
id org_id
1 1
2 1
3 2
4 2
answer_sheets
=============
id person_id survey_id answer date_answered
1 1 1 string JUN 13
2 2 1 string JUN 15
3 3 2 string JUN 17
4 2 2 string JUN 18
5 1 2 string JUN 19
6 3 3 string JUN 20
7 2 3 string JUN 25
8 4 3 string JUN 27
9 4 4 string JUN 27
我想根据一行最新的调查people
按顺序订购= 1ASC
people
answer_sheets.date_answered
org_id
输出将是
=============
id name last_date_answered
4 Person4 NIL
5 Person5 NIL
3 Person3 JUN 17
2 Person2 JUN 18
1 Person1 JUN 19
您可以观察到people
with id
s 4 和 5 没有answer_sheet
in surveys
with org_id
= 1 ,但它们应该包含在列表中。问题:什么必须是适当的 SQL 查询?谢谢。