sql - 将两列合二为一
问题描述
我有两列(user_from 和 user_to),我需要知道有多少不同的用户出现在我的数据库中。有什么好的和快速的方法来做到这一点?顺便说一句,我正在使用 PostgreSQL。
解决方案
此查询足以获取用户列表:
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;
推荐阅读
- sqlite - SQLite Epoch 时间查询
- algorithm - 二和及其时间复杂度
- node.js - mongodb db没有出现
- apache-spark - 将列组合成键、值对列表(无 UDF)
- reactjs - React Hooks 和 React 生命周期方法
- matlab - 使用 QR 分解 (MATLAB) 求解线性回归模型
- html - 在 Github Flavored Markdown 中水平居中表格
- python-3.x - DHCP嗅探python3
- angular - MatDialog 的背景在 Angular 中显示为灰色板
- c# - 如何在 ResourceDictionary 的 CombinedGeometry-tag 中使用从 InkScape 导出的 Xaml 多边形