mysql - 避免在 SELECT 查询 MYSQL 中重复输入

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

最佳答案

你可以这样做

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/

相关文章:

mysql - 选择IP范围内的CIDR

MYSQL 不同的随机连接查询

MySQL 查询提取电子邮件邻居

php pdo使用变量显示mysql查询的输出

mysql - 为什么 ON DELETE CASCADE 不删除引用的记录?

mysql - 如何为数字选择第一个唯一 ID

mysql - 从表连接中选择不同的有序对

c# - 在 C# 中使用 LINQ 获取 List<T> 中不同属性值的数量

MySQL JOIN/UNION DISTINCT 问题

python - Python 3 中的 SQLAlchemy "def print_usage()"错误