如何从列中获取重复值 [英] how to fnd a duplicate values from a column

查看:61
本文介绍了如何从列中获取重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过select语句显示从列中找到重复值..

就像我有这张桌子



stid stname

1 rahul

2 amit

3 abhi

4 rahul

5 rahul





现在我想要这样的节目。



stid stname

1 rahul

4 rahul

5 rahul



请帮我解决这个问题。

解决方案

试试这个

< pre lang =sql> DECLARE @ t TABLE (stid INT ,stname VARCHAR 10 ))
INSERT INTO @ t (stid,stname) VALUES 1 ' 1'
INSERT INTO @ t (stid,stname) VALUES 2 ' 1'
INSERT INTO @ t (stid,stname) VALUES 3 ' 2'
INSERT INTO @ t (stid,stname) VALUES 4 ' 3'
INSERT INTO @ t (stid,stname) VALUES (< span class =code-digit> 5 ,' 4'
INSERT INTO @ t (stid,stname) VALUES 6 ' 5'
INSERT INTO @ t (stid) ,stname) VALUES 7 ' 6'


选择 * 来自 @ t 其中​​ stname IN SELECT stname FROM @ t GROUP BY stname HAVING COUNT (stname)> 1


阅读以下内容:http://stackoverflow.com/questions/2594829/finding-duplicate-values-in-a-sql-table [ ^ ]

i want to show through select statement to find a duplicate value from a column ..
like i am having this table

stid stname
1 rahul
2 amit
3 abhi
4 rahul
5 rahul


now i want show like that.

stid stname
1 rahul
4 rahul
5 rahul

please help me out from this problem.

解决方案

try this

DECLARE @t TABLE (stid  INT, stname VARCHAR(10))
INSERT INTO @t(stid, stname) VALUES (1, '1')
INSERT INTO @t(stid, stname) VALUES (2, '1')
INSERT INTO @t(stid, stname) VALUES (3, '2')
INSERT INTO @t(stid, stname) VALUES (4, '3')
INSERT INTO @t(stid, stname) VALUES (5, '4')
INSERT INTO @t(stid, stname) VALUES (6, '5')
INSERT INTO @t(stid, stname) VALUES (7, '6')


select * from @t where stname IN ( SELECT stname FROM @t GROUP BY stname HAVING COUNT(stname) > 1)


Read the following : http://stackoverflow.com/questions/2594829/finding-duplicate-values-in-a-sql-table[^]


这篇关于如何从列中获取重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆