日期:2014-05-18  浏览次数:20369 次

去掉DataSet中的重复字段
早上发了一贴,把三张表的数据存到一个DataSet中,这个已经做到,可是uname这个字段中有重复的数据,怎么样能把这些重复数据去掉呢?

------解决方案--------------------
SqlDataAdapter da = new SqlDataAdapter("select distinct uname from test", conn); 
SqlDataAdapter da1 = new SqlDataAdapter("select distinct uname from test1", conn); 
SqlDataAdapter da2 = new SqlDataAdapter("select distinct uname from test2", conn); 

------解决方案--------------------
SQL code
select uname from t1
union
select uname from t2
union
select union from t3

------解决方案--------------------
顶。在Sql里处理。。。。。
不取重复值。可以用EXISTS那个条件判断下在查询出来。
------解决方案--------------------
DataSet的绑定原来只是一个数据绑定,不可能智能的去掉重复数据
LZ又说不能使用Sql判断,还请你教我
继续学习
select uname from t1
union
select uname from t2
union
select uname from t3
联合查询后执行数据DataSet
------解决方案--------------------
我比较苯的做法
select uname from t1
select uname from t2 where uname not in (select uname from t1)
select uname from t3 where uname not in (select uname from t1) and uname not in (select uname from t2)
------解决方案--------------------
我看dataSet里有一个Select()的方法,这个能不能去掉呢?

不行.
datatable.select方法只能指定filter即过滤条件,而不能对 选择列 进行确定

以下都是随手敲的,难免手误

select id,name from tb where id>1
即,select()方法只能指定 蓝色的部分,不能确定红色的部分.

如果是在sql中,可以通过 where 后变通的方法来使 相同的name只取一个.
比如,设id为identity列.

SQL code
select id,name from tb a where not exits(select 1 from tb where name=a.name and id<a.id) --name相同的取最小的id. 或取最大id将<改为>即可
--或是
select id,name from tb a where id in(select top 1 id from tb where name=a.name order by id)
--或是
select id,name from tb a where id=(select max(id) from tb where name=a.name)
--或是
select a.id,a.name from tb a inner join(select max(id) mi from tb group by name ) b where mi=id
--或是
select id,name from tb a where 1=(select count(1) from tb where name=a.name and id<a.id)

------解决方案--------------------
dt.Select(双引name=单引双引 + ... + 双引单引双引);