从两列中查找重复项,但显示所有行 MySQL

Finding duplicates from two columns, but show all rows MySQL

我有一个table这样的

| user_id | company_id | employee_id |
|---------|------------|-------------|
|    1    |      2     |     123     |
|    2    |      2     |     123     |
|    3    |      5     |     432     |
|    4    |      5     |     432     |
|    5    |      7     |     432     |

我有一个类似这样的查询

SELECT COUNT(*) AS Repeated, employee_id, GROUP_CONCAT(user_id) as user_ids, GROUP_CONCAT(username)
    FROM user_company
    INNER JOIN user ON user.id = user_company.user_id
        WHERE employee_id IS NOT NULL
        AND user_company.deleted_at IS NULL
        GROUP BY employee_id, company_id
        HAVING Repeated >1;

我得到的结果是这样的

| Repeated | employee_id | user_ids   |
|---------|--------------|------------|
|    2    |      123     |   2,3      |
|    2    |      432     |   7,8      |

我需要这样的结果

| user_id |
|---------|
|    2    |
|    3    |
|    7    |
|    8    |

我意识到我的查询越来越多,但这只是为了确保我得到正确的数据。现在我需要在新行中获取每个 user_id 的单列结果,以便根据另一个查询中的 user_id 进行更新。我已经尝试过只选择 user_id 但我只得到两行,我需要所有四行重复项。

关于如何修改我的查询有什么想法吗?

这是获取所有 user_ids:

的查询
SELECT user_id 
FROM user_company uc 
INNER JOIN 
( 
   SELECT employee_id, company_id
   FROM user_company 
   WHERE employee_id IS NOT NULL
   AND deleted_at IS NULL
   GROUP BY employee_id, company_id
   HAVING COUNT(employee_id) >1
) AS `emps`
ON emps.employee_id = uc.`employee_id`
AND emps.company_id = uc.`company_id`;

下面的查询将生成您要查找的查询。

    SELECT CONCAT('UPDATE user_company SET employee_id = null WHERE user_id IN (', GROUP_CONCAT(user_id SEPARATOR ', '),')') AS user_sql
    FROM    user_company uc
    INNER JOIN 
    (SELECT employee_id, company_id
       FROM user_company 
       WHERE employee_id IS NOT NULL
       AND deleted_at IS NULL
       GROUP BY employee_id, company_id
       HAVING COUNT(employee_id) >1) AS `emps`
       ON emps.employee_id = uc.`employee_id`
       AND emps.company_id = uc.`company_id`;