如何通过组合来自 3 个不同表的数据来获得如下输出
How to get the output as following by combining data from 3 different tables
所以我的数据库中有 3 个 table,其中所有 3 个 table 都包含一个具有相似数据的列,但该列的名称在所有 3 tables,下面是一个例子。
禁止 Table
user_id
ban_user_id
ban_date
reason
end_date
1300
1
xyz
xyz
xyz
32
1
xyz
xyz
xyz
43
2
xyz
xyz
xyz
报告Table
user_id
last_modified_user_id
report_date
reason
end_date
1300
1
xyz
xyz
xyz
32
2
xyz
xyz
xyz
43
2
xyz
xyz
xyz
警告Table
user_id
warning_user_id
warning_date
reason
end_date
1300
1
xyz
xyz
xyz
32
2
xyz
xyz
xyz
43
3
xyz
xyz
xyz
现在我想把这3个table组合起来取数据,其中ban_user_id、last_modified_user_id、warning_user_id是拿的工作人员的数据操作,所以我想按员工 ID 对数据进行分组。
我要找的输出如下:
staff_id
total_reports
total_bans
total_warnings
1
1
2
1
2
2
1
1
3
0
0
1
它通过对第 2 列 ban_user_id、last_modified_user_id、warning_user_id 进行分组来计算每个 table 的数据。而不是合并数据。
我尝试了 UNION All 之类的东西,但没有成功。
提前感谢您的帮助
您可以使用 Table joins (Inner/outer/left/right) 来获取数据而不是并集。
我假设 staff_id 等同于 user_id 列,因为你没有提到任何相关内容,所以你的脚本看起来像这样:
SELECT W.user_id AS staff_id,
B.ban_user_id,
R.last_modified_user_id,
W.warning_user_id
FROM Warning AS W
LEFT JOIN Reports AS R on R.user_id = W.user_id
LEFT JOIN Ban AS B on B.user_id = W.user_id
group by W.user_id
对所有 3 个表使用 UNION ALL
,然后聚合:
SELECT staff_id,
COUNT(report) AS total_reports,
COUNT(ban) AS total_bans,
COUNT(warning) AS total_warnings
FROM (
SELECT last_modified_user_id AS staff_id, 1 AS report, null AS ban, null AS warning FROM Reports
UNION ALL
SELECT ban_user_id, null, 1, null FROM Ban
UNION ALL
SELECT warning_user_id, null, null, 1 FROM Warning
) t
GROUP BY staff_id;
或者:
SELECT staff_id,
SUM(report) AS total_reports,
SUM(ban) AS total_bans,
SUM(warning) AS total_warnings
FROM (
SELECT last_modified_user_id AS staff_id, 1 AS report, 0 AS ban, 0 AS warning FROM Reports
UNION ALL
SELECT ban_user_id, 0, 1, 0 FROM Ban
UNION ALL
SELECT warning_user_id, 0, 0, 1 FROM Warning
) t
GROUP BY staff_id;
参见demo。
所以我的数据库中有 3 个 table,其中所有 3 个 table 都包含一个具有相似数据的列,但该列的名称在所有 3 tables,下面是一个例子。
禁止 Table
user_id | ban_user_id | ban_date | reason | end_date |
---|---|---|---|---|
1300 | 1 | xyz | xyz | xyz |
32 | 1 | xyz | xyz | xyz |
43 | 2 | xyz | xyz | xyz |
报告Table
user_id | last_modified_user_id | report_date | reason | end_date |
---|---|---|---|---|
1300 | 1 | xyz | xyz | xyz |
32 | 2 | xyz | xyz | xyz |
43 | 2 | xyz | xyz | xyz |
警告Table
user_id | warning_user_id | warning_date | reason | end_date |
---|---|---|---|---|
1300 | 1 | xyz | xyz | xyz |
32 | 2 | xyz | xyz | xyz |
43 | 3 | xyz | xyz | xyz |
现在我想把这3个table组合起来取数据,其中ban_user_id、last_modified_user_id、warning_user_id是拿的工作人员的数据操作,所以我想按员工 ID 对数据进行分组。
我要找的输出如下:
staff_id | total_reports | total_bans | total_warnings |
---|---|---|---|
1 | 1 | 2 | 1 |
2 | 2 | 1 | 1 |
3 | 0 | 0 | 1 |
它通过对第 2 列 ban_user_id、last_modified_user_id、warning_user_id 进行分组来计算每个 table 的数据。而不是合并数据。
我尝试了 UNION All 之类的东西,但没有成功。
提前感谢您的帮助
您可以使用 Table joins (Inner/outer/left/right) 来获取数据而不是并集。 我假设 staff_id 等同于 user_id 列,因为你没有提到任何相关内容,所以你的脚本看起来像这样:
SELECT W.user_id AS staff_id,
B.ban_user_id,
R.last_modified_user_id,
W.warning_user_id
FROM Warning AS W
LEFT JOIN Reports AS R on R.user_id = W.user_id
LEFT JOIN Ban AS B on B.user_id = W.user_id
group by W.user_id
对所有 3 个表使用 UNION ALL
,然后聚合:
SELECT staff_id,
COUNT(report) AS total_reports,
COUNT(ban) AS total_bans,
COUNT(warning) AS total_warnings
FROM (
SELECT last_modified_user_id AS staff_id, 1 AS report, null AS ban, null AS warning FROM Reports
UNION ALL
SELECT ban_user_id, null, 1, null FROM Ban
UNION ALL
SELECT warning_user_id, null, null, 1 FROM Warning
) t
GROUP BY staff_id;
或者:
SELECT staff_id,
SUM(report) AS total_reports,
SUM(ban) AS total_bans,
SUM(warning) AS total_warnings
FROM (
SELECT last_modified_user_id AS staff_id, 1 AS report, 0 AS ban, 0 AS warning FROM Reports
UNION ALL
SELECT ban_user_id, 0, 1, 0 FROM Ban
UNION ALL
SELECT warning_user_id, 0, 0, 1 FROM Warning
) t
GROUP BY staff_id;
参见demo。