首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

DISTINCT有关问题

2012-04-28 
DISTINCT问题有tb1表code name0001 10002 20001 1NULL 1000002 20003 3想得到TB2表code name0001 10002 20

DISTINCT问题
有tb1表
code name
0001 1
0002 2
0001 1
NULL 100
0002 2
0003 3

想得到TB2表
code name
0001 1
0002 2
0003 3

用insert into tb2 (code,name) select DISTINCT code,name from tb1不行,
用insert into tb2 code select DISTINCT code from tb1就可以,但只有一列!


[解决办法]

SQL code
insert into tb2 (code,name) select code,min(name) from tb1 where code is not null or upper(code)!='NULL'group by code
[解决办法]
insert into tb2 (code,name)
select code,min(name) from tb1 where code is not null group by code,name

热点排行