[英]how to combine two 'count' value from different tables (2 column, NOT union)
I want to get the count of addmited records by user in 2 table. 我想在2表中获取用户添加的记录数。 for example, I have a list of users(table STF) and want to know how many product did a user create (in table PV1) and how many product did he sell (in table dpq), I want to show these data like below:
例如,我有一个用户列表(表STF),想知道用户创建了多少产品(在表PV1中)以及他销售了多少产品(在表dpq中),我想显示这些数据,如下所示:
I have these 2 queries, and don't know how to show them in one table with 3 columns... 我有这2个查询,不知道如何在一个3列的表格中显示它们...
Query 1: 查询1:
select staff_username, COUNT(*) as 'count 1'
from STF right join PV1 on STF.staff_username = PV1.admit_user
group by staff_username, staff_name + ' ' + staff_family
Query 2: 查询2:
select trf_staff_id, COUNT(trf_staff_id)
from dpq join stf on trf_staff_id = stf.staff_username
group by trf_staff_id
I guess you want to combine two table count
,You could join
and add count
我想你想结合两个表
count
,你可以join
并添加count
but you give us less information so I'm not sure. 但是您给我们的信息较少,所以我不确定。
SELECT
STF.staff_username,
STF.Data_in_PV1,
dpq.Data_in_dpq
FROM
(
SELECT
staff_username,
COUNT(PV1.admit_user) AS 'Data_in_PV1'
FROM
STF
RIGHT JOIN
PV1
ON
STF.staff_username = PV1.admit_user
GROUP BY
staff_username
) STF
INNER JOIN
(
SELECT
dpq.trf_staff_id,
COUNT(trf_staff_id) AS 'Data_in_dpq'
FROM
dpq
INNER JOIN
stf
ON
dpq.trf_staff_id = stf.staff_username
GROUP BY
dpq.trf_staff_id
) dpq ON dpq.trf_staff_id = STF.staff_username
You can try joining two queries like this. 您可以尝试像这样联接两个查询。 From limited info which you have provided it seems that staff_username is same as trf_staff_id as you have used it in join condition of second query.
根据您提供的有限信息,似乎staff_username与trf_staff_id相同,因为您在第二个查询的联接条件中使用了它。
SELECT
staff_username,
count_1,
count_2 FROM
(
SELECT
staff_username,
COUNT(*) AS count_1
FROM
STF
RIGHT JOIN
PV1
ON
STF.staff_username = PV1.admit_user
GROUP BY
staff_username,
staff_name + ' ' + staff_family ) QRY_CNT_1 INNEER JOIN
(
SELECT
trf_staff_id,
COUNT(trf_staff_id) AS count_2
FROM
dpq
JOIN
stf
ON
trf_staff_id = stf.staff_username
GROUP BY
trf_staff_id ) QRY_CNT_2 ON
QRY_CNT_2.trf_staff_id = QRY_CNT_1.staff_username
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.