易截截图软件、单文件、免安装、纯绿色、仅160KB

这个 sql删除语句怎么写? - MS-SQL Server / 基础类

SQL code:
declare @tb1 table(a varchar(20),b varchar(20))
insert into @tb1 select 'a101','b11121'
insert into @tb1 select 'a102','b00012'
select * from @tb1

declare @tb2 table(id int,c varchar(20))
insert into @tb2 select 1,'a101'
insert into @tb2 select 2,'b11121'
insert into @tb2 select 3,'a000'
insert into @tb2 select 4,'c1001'
select * from @tb2

我现在要把 @tb2 中c字段 在 @tb1 中有重复的数据删除,保留@tb2 中id 较小的

@tb1中 'a101',' b11121' 在同一行,
而@tb2 中c字段有
1 a101
2 b11121
3 a000
4 c1001

就删除第二行,最后得到结果
1 a101
3 a000
4 c1001

这个sql怎么写?

修改一下

SQL code:
declare @tb1 table(id int,a varchar(20),b varchar(20))
insert into @tb1 select 1,'a101','b11121'
insert into @tb1 select 2,'a102','b00012'
select * from @tb1

declare @tb2 table(id int,c varchar(20))
insert into @tb2 select 1,'a101'
insert into @tb2 select 2,'b11121'
insert into @tb2 select 3,'a000'
insert into @tb2 select 4,'c1001'
select * from @tb2

两表id都是唯一的

我现在要把 @tb2 中c字段 在 @tb1 中有重复的数据删除,保留@tb2 中id 较小的

@tb1中 'a101',' b11121' 在同一行,
而@tb2 中c字段有
1 a101
2 b11121
3 a000
4 c1001

就删除第二行,最后得到结果
1


相关问答:

SQL如何优化问题 - MS-SQL Server / 疑难问题

今天做了一个存储过程   环境是SQL2000数据库  
大致如下
建立临时表
定义员工游标
        循环员工(属于1个公司)  
        ......

数据以xml格式返回 - MS-SQL Server / 应用实例

从数据库中查询一张表的数据
select 部门,姓名 from tb
如何才能生成下面的xml格式
XML code:
<folder state="unchecked" label="全部">
   <folder state="unchecked&qu ......

请教SQLSERVER的两个问题 - MS-SQL Server / 疑难问题

1。怎样使xp_cmdshell能完整输出超过255个字符的字符串。
2。select 时,检索速度是与from后的 TABLE顺序有关,还是与where条件的顺序有关(TABLE数据多少 )
在系统属性设定里有个选项,可以修改单字段输出字数限制. ......

SQL不明白 - MS-SQL Server / 应用实例

查询学生平均成绩及其名次 
  SELECT 1+(SELECT COUNT( distinct 平均成绩) 
  from (SELECT S#,AVG(score) AS 平均成绩 
  from SC&n ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号