首页 > 解决方案 > 将两列合二为一

问题描述

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

标签: sqlpostgresql

解决方案


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

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;

推荐阅读