[英]How to query mysql to select rows with non unique combination of two columns
I have the following table:我有下表:
| id | user_id | game_id |
|----|---------|---------|
| 83 | 1 | 1 |
| 84 | 1 | 1 |
| 85 | 1 | 1 |
| 86 | 2 | 2 |
| 87 | 2 | 2 |
| 88 | 3 | 2 |
| 89 | 10 | 2 |
| 90 | 1 | 3 |
| 91 | 11 | 3 |
I do not want to select the rows, which have only one distinct user for a game (rows 1-3).我不想选择只有一个不同用户玩游戏的行(第 1-3 行)。 The goal is to identify which users are connected via a game.
目标是确定哪些用户是通过游戏连接的。 So I would like to select only rows if there are not just unique combinations of the user_id with the game_id.
因此,如果 user_id 与 game_id 的组合不唯一,我只想选择行。 The desired result should look like one of the following tables:
所需的结果应类似于下表之一:
1. including duplicates 1.包括重复
| id | user_id | game_id |
|----|---------|---------|
| 86 | 2 | 2 |
| 87 | 2 | 2 |
| 88 | 3 | 2 |
| 89 | 10 | 2 |
| 90 | 1 | 3 |
| 91 | 11 | 3 |
2. without duplicates 2.无重复
| id | user_id | game_id |
|----|---------|---------|
| 86 | 2 | 2 |
| 88 | 3 | 2 |
| 89 | 10 | 2 |
| 90 | 1 | 3 |
| 91 | 11 | 3 |
I guess, I have to use a combination of the HAVING and GROUP BY clause.我想,我必须结合使用 HAVING 和 GROUP BY 子句。 But unfortunately, the following query does not work:
但不幸的是,以下查询不起作用:
SELECT id, game_id, user_id
FROM table
AND (game_id, user_id) IN
(SELECT game_id, user_id
FROM table
GROUP BY game_id, user_id
HAVING COUNT(*) > 1)
Does anyone know what I am missing?有谁知道我错过了什么?
You can try this -你可以试试这个 -
SELECT min(id) as id, game_id, user_id
FROM table
group by game_id, user_id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.