我有一个名为member.id的列,它出现在一个名为poster的表中,作为poster.sender和poster.reciever。
我需要一个选择member.value的查询,其中member.id是poster.sender或poster.reciever。
[我知道名字没有多大意义 - 我没有想出来]
到目前为止,我有这个,它不起作用:
SELECT member.value
FROM ((poster INNER JOIN member ON poster.sender = member.ID)
INNER JOIN poster ON poster.sender= member.id
WHERE (member.id <> ?
AND (((poster.Sender = ?) OR (poster.reciever = ?))AND (poster.Status = 'Accepted'))
任何想法都会非常有用!
答案 0 :(得分:0)
试试这个:
SELECT
member.value
FROM
poster s INNER JOIN member ON s.sender = member.ID
INNER JOIN poster r ON r.receiver = member.id
WHERE
member.id <> ?
AND
(
s.sender = ?
OR r.reciever = ?
)
AND
s.status = 'Accepted'
在没有别名的查询中,您不能使用两次表,因此别名为s
和r
。
根据您的要求改变条件 - &gt;发件人或收件人表中的status = 'Accepted'
或两者。
答案 1 :(得分:0)
你会想要这样的东西......
select distinct(value)
from
(
(select member.value from member inner join poster on member.id = poster.sender)
union
(select member.value from member inner join poster on member.id = poster.receiver)
)
答案 2 :(得分:0)
SELECT value
FROM member
WHERE EXISTS (SELECT *
FROM poster
WHERE member.id IN (poster.sender,
poster.receiver));
应该相当于N Mason,但可能更快。至少它不需要消除UNION
的重复项(希望优化器忽略外部DISTINCT
,因为UNION
的结果已经是明显的并且不会再次无用地执行此操作。消除重复可能是昂贵的。