我有下表
| tableid | Email | date |Prymary Record|
| 1 | x | 12-DEC-09 | Yes |
| 1 | y | 12-DEC-09 | No |
| 1 | y | 13-DEC-09 | No |
| 2 | z | 14-DEC-09 | Yes |
| 3 | W | 14-DEC-09 | Yes |
| 4 | W | 14-DEC-09 | Yes |
| 4 | W | 14-DEC-09 | No |
我想从这个表中获取tableid重复不止一次的所有列和行
答案 0 :(得分:2)
以下是一句话:
select * from mytable
where id in (
select id from mytable
group by id
having count(*) > 1)
答案 1 :(得分:2)
如果你在SQL Server中,你也可以这样做:
select *
from (
select *
, cnt = count(*) over (partition by tableid)
from mytable
) x
where x.cnt > 1;