我有一个有4列(id,发件人,收件人,状态)的朋友,我需要一个查询,它将联合发送者和接收者的发送者和接收者列结合在一起(重聚,我不知道英文单词)等于让我们说2。 例如,表格如下所示
| sender | receiver |
| 2 | 10 |
| 2 | 8 |
| 2 | 9 |
| 6 | 2 |
| 7 | 3 |
| 10 | 3 |
在查询之后它将显示:(显示前3行因为sender = 2,然后第四行显示,因为接收器为2.其余行未显示)
| result |
| 10 |
| 8 |
| 9 |
| 6 |
答案 0 :(得分:1)
你几乎拥有正确的关键字; UNION会让你做你想做的事。
SELECT `receiver` as `result` FROM `table` WHERE `sender` = 2
UNION
SELECT `sender` as `result` FROM `table` WHERE `receiver` = 2
答案 1 :(得分:1)
select
case when sender = 2
then receiver
else sender
end
from friends
where sender = 2 or receiver = 2
答案 2 :(得分:1)
这是您的查询,我希望对您有意义:
Select sender as result
from friends
where receiver = 2
union
Select receiver as result
from friends
where sender = 2
答案 3 :(得分:0)
SELECT *
FROM
(SELECT sender as iResult
FROM friends
WHERE receiver = 2
UNION
SELECT receiver as result
FROM friends
WHERE sender = 2) c
ORDER BY c.iResult DESC