如何合并来自不同表的两个 'count' 值(2 列,非并集)

how to combine two 'count' value from different tables (2 column, NOT union)

我想获取 2 table 中用户添加的记录数。例如,我有一个用户列表(table STF)并且想知道用户创建了多少产品(在 table PV1 中)以及他销售了多少产品(在 table dpq),我想像下面这样显示这些数据:

我有这 2 个查询,但不知道如何在一个 table 3 列中显示它们...

查询 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

查询 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

我猜你想合并两个 table count,你可以 join 然后添加 count

但是你给我们的信息较少,所以我不确定。

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

您可以尝试像这样连接两个查询。从您提供的有限信息看来 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