我有三个表:人,消息和hasfriend。现在,我需要向每个人显示具有相同数量的朋友并发送邮件的人。
人员拥有所有人的信息,最重要的是电子邮件。邮件中有一个名为“ fromemail”的列,该列显示了发送邮件的人,而hasfriend也有一个“ email”列,它引用了该表格的人。
答案 0 :(得分:0)
您可以尝试以下方式:
SELECT p1.email, p2.email
FROM
(SELECT email, count(message_id) m_count, count(friend_id) f_count
FROM
person p
JOIN message m ON p.email = m.email
JOIN hasfriend f ON p.email = f.email) p1,
(SELECT email, count(message_id) m_count, count(friend_id) f_count
FROM
person p
JOIN message m ON p.email = m.email
JOIN hasfriend f ON p.email = f.email) p2
WHERE
p1.email = p2.email AND
p1.m_count = p2.m_count AND
p1.f_count = p2.f_count;