我想要做的是每次他们推荐某人时列出一个条目,并且那个人也列在条目表中。我尝试过的3个不同的查询是:
SELECT a.ID,b.Email FROM Entries a
INNER JOIN ReferAFriend b ON b.EntryID = a.ID
INNER JOIN (SELECT DISTINCT Email FROM Entries c ON b.Email = c.Email)
WHERE a.ID = 47667
SELECT a.ID,b.Email FROM Entries a
INNER JOIN ReferAFriend b ON b.EntryID = a.ID AND b.Email IN (SELECT DISTINCT Email FROM Entries)
WHERE a.ID = 47667
SELECT a.ID,b.Email FROM Entries a
INNER JOIN ReferAFriend b ON b.EntryID = a.ID
WHERE b.Email IN (SELECT DISTINCT Email FROM Entries) AND a.ID = 47667
所有3的结果只是一个条目。
如果我这样做:
SELECT a.ID,b.Email FROM Entries a
INNER JOIN ReferAFriend b ON b.EntryID = a.ID
WHERE a.ID = 47667
我得到了20个条目的列表,因为有20个被引用的朋友,但是条目数据库中只有4个,这是另一个查询应该从这20个中过滤掉的,但它只返回1个结果。
有人能指出我在正确的方向吗?
感谢。
答案 0 :(得分:0)
略有重组...
SELECT DISTINCT STRAIGHT_JOIN
e1.ID,
rf.Email
FROM
Entries e1
JOIN ReferAFriend rf
on e1.ID = rf.EntryID
JOIN Entries e2
on rf.EMail = e2.EMail
where
e1.ID = 47667
您可以查询并显示...
的结果SELECT DISTINCT
e1.ID,
rf.Email
FROM
Entries e1
JOIN ReferAFriend rf
on e1.ID = rf.EntryID
where
e1.ID = 47667
答案 1 :(得分:0)
WHERE子句将结果过滤为一个。改变了,它的确有效。