Postgresql -从SELECT中排除一些结果
Postgresql - exclude some results from SELECT
我有一个名为"friendship"的表,它的列是这样的:
|from|to|status|其中"from"answers"to"引用用户名,"status"为"a"表示接受,"d"表示拒绝,"p"表示挂起。
我想从一个用户中选择所有的朋友,并把他们放在一个单独的列。这可能吗?
为了认识所有的用户朋友,我做了这样的事情:
SELECT to,from,status FROM friendship
WHERE to = 'john' AND status = 'a'
OR from = 'john' AND status = 'a'
现在我需要某种方法获得除'john'之外的所有名称,并将它们放在单独的列中。
我也在用c++做这个…那么,我可以使用PQgetvalue来帮助我实现这一点吗?
您可以使用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
,如果产生dps):
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"是一个糟糕的列名…(这是一个保留词)
可以使用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';
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 在更改for循环的第三部分后,未使用for循环结果
- 使用++运算符会导致意外的结果
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- C++Brute Force攻击函数不会返回结果
- 你好。。。id_public变量不应该给出结果为 81 和 86 吗?为什么它为两个派生类占用不同的内存位置?
- 算术运算的结果类似于:C浮点变量中的1/3
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 密码登录程序将永远循环并显示不正确的结果
- 如何让C++'tally up'结果并制定计划?
- 有没有办法排除不可调用函数的结果
- Postgresql -从SELECT中排除一些结果