我有一个SQL表:
+-------------+-----------+---------+
| ID | position | user |
+-------------+-----------+---------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
| 6 | 6 | 0 |
| 7 | 7 | 0 |
| 8 | 7 | 1 |
+-------------+-----------+---------+
我想基于位置列和用户列的不同值过滤重复行,对于第一个查询我需要得到以下结果:
+-------------+-----------+---------+
| ID | position | user |
+-------------+-----------+---------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
| 6 | 6 | 0 |
| 8 | 7 | 1 |
+-------------+-----------+---------+
对于第二个查询,我需要以下内容:
+-------------+-----------+---------+
| ID | position | user |
+-------------+-----------+---------+
| 1 | 1 | 0 |
| 2 | 2 | 0 |
| 3 | 3 | 0 |
| 4 | 4 | 0 |
| 5 | 5 | 0 |
| 6 | 6 | 0 |
| 7 | 7 | 0 |
+-------------+-----------+---------+
我需要哪些查询来实现这一目标?
感谢。
答案 0 :(得分:1)
如果没有进一步的信息,下面的两个查询假设你想要通过采用较大(最大)user
值(在第一种情况下)或较小(最小){{}来解决重复位置。 1}}在第二种情况下的值。
首先查询:
user
第二次查询:
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT position, MAX(user) AS max_user
FROM yourTable
GROUP BY position
) t2
ON t1.position = t2.position AND
t1.user = t2.max_user