避免在SELECT查询MYSQL中双重输入

时间:2012-08-18 09:51:14

标签: mysql select distinct

如何避免在以下查询中列出多个记录?

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条记录不重复

1 个答案:

答案 0 :(得分: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)选择每个音符的最后一条记录并将其连接到外部查询行