这是我用于在一个文件夹中显示电子邮件的MySQL查询(当然是精简的)......
SELECT em.id
FROM email_messages AS em
LEFT JOIN email_attachments AS ea on ea.id_email=em.id
WHERE em.id='4'
问题是,如果电子邮件包含多个附件,则相同的电子邮件会出现两次,例如email_message
和id
4。
如果不更改SQL表的结构,如何返回任何值(例如1)来表示电子邮件具有附件而不返回重复的行?
答案 0 :(得分:1)
添加DISTINCT
以仅获取唯一值
SELECT DISTINCT em.id
FROM email_messages AS em
LEFT JOIN email_attachments AS ea
ON ea.id_email = em.id
WHERE em.id = '4'
或GROUP BY
条款
SELECT em.id
FROM email_messages AS em
LEFT JOIN email_attachments AS ea
ON ea.id_email = em.id
WHERE em.id = '4'
GROUP BY em.id