我想编写一条 SQL 语句,但遇到麻烦。
假设我有一个包含以下数据的表:
id | ip |
---|---|
1 | 1 |
1 | 1 |
1 | 1 |
2 | 1 |
2 | 1 |
3 | 2 |
3 | 2 |
4 | 2 |
4 | 2 |
4 | 2 |
5 | 3 |
5 | 3 |
6 | 4 |
6 | 4 |
7 | 4 |
7 | 4 |
我希望在此表上查询的结果是:
id | ip |
---|---|
1 | 1 |
2 | 1 |
3 | 2 |
4 | 2 |
6 | 4 |
7 | 4 |
如您所见,它省略了 5 3 数据,因为它没有多个具有相同 ip 的 id。是否需要进行嵌套?
抱歉,已尝试:
select distinct poster_id, poster_ip from
`phpbbkp_posts` where poster_id in
(select poster_id
from
(select distinct poster_id, poster_ip from `phpbbkp_posts`) p
group by poster_id
having count(*) > 1)
order by poster_id
没用。
这对我来说只是一个与我所计算的相反的逻辑
SELECT poster_id, poster_ip FROM `phpbbkp_posts`
WHERE poster_ip
IN(
SELECT poster_ip
FROM (
SELECT DISTINCT poster_id, poster_ip FROM `phpbbkp_posts`
) AS P
GROUP BY poster_ip
HAVING COUNT(poster_id) > 1
)