我有一个名为“友谊”的表,其列如下:
|从|到|状态|其中“from”和“to”引用了用户名,“status”是“a”表示接受,“d”表示拒绝,“p”表示待处理。
我想从用户中选择所有朋友并将它们放在一个单独的列中..是否可能?
要了解所有用户朋友,我做了类似的事情:
SELECT to,from,status FROM friendship
WHERE to = 'john' AND status = 'a'
OR from = 'john' AND status = 'a'
现在我需要获得除“约翰”之外的所有名字并将它们放在一个单独的列中。
我也是用c ++做的。所以有什么方法可以用PQgetvalue来帮我实现这个目的吗?
答案 0 :(得分:1)
您可以使用case
声明:
select case
when "to" = 'john' then "from"
else "to"
end as friend
from friendship
where status = 'a'
and ("from" = 'john' or "to" = 'john')
或union all
(或union
,如果这会产生重复):
select "to" as friend
from friendship
where status = 'a'
and "from" = 'john'
union all
select "from" as friend
from friendship
where status = 'a'
and "to" = 'john'
作为旁注,“from”是一个糟糕的专栏名称......(这是一个保留字。)
答案 1 :(得分:0)
您可以使用UNION运算符
SELECT "to" as name FROM "friendship"
WHERE "from" = "john" AND "status" = 'a'
UNION
SELECT "from" as name FROM "friendship"
WHERE "to" = 'john' AND "status" = 'a';