将两列合为一

时间:2018-10-15 16:24:11

标签: sql postgresql

我有两列(user_from和user_to),我需要知道数据库中出现了多少个不同的用户。有什么好方法可以快速做到这一点? 我正在使用PostgreSQL,顺便说一句。

2 个答案:

答案 0 :(得分:1)

rangestat

答案 1 :(得分:1)

此查询足以获取用户列表:

select user_from as UserName
from t
union   -- intentional to remove duplicates
select user_To as UserName
from t;

如果要计数,则:

select count(*)
from (select user_from as UserName
      from t
      union 
      select user_To as UserName
      from t
     ) t;