如何避免在以下查询中列出多条记录?
SELECT DISTINCT application_forms.date_submitted,
application_forms.application_type,
application_forms.online_offline2,
application_forms.decision,
application_forms.id,
notes.post_by_id
FROM application_forms
LEFT JOIN notes
ON notes.user_id = application_forms.id
我想要列出 1 条不重复的记录
最佳答案
你可以这样做
SELECT application_forms.date_submitted,
application_forms.application_type,
application_forms.online_offline2,
application_forms.decision,
application_forms.id,
n.post_by_id
FROM application_forms
LEFT JOIN (SELECT user_id , MAX(id) , notes.post_by_id FROM notes) as n
ON n.user_id = application_forms.id
GROUP BY application_forms.date_submitted
在左连接中看到 MAX(id) 选择每个注释的最后一条记录并将其连接到外部查询行
关于mysql - 避免在 SELECT 查询 MYSQL 中重复输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12017602/