工作中用到的sql语名

查询重复非唯一行sql:

SELECT DISTINCT 字段 FROM 表 --选出不重复值 
select ID from table1 group by ID HAVING COUNT(*)>1--选出重复值


删除重复数据,留一条:

delete from xprice_srchannelrate
 where ratecode in
       (select ratecode from xprice_srchannelrate group by ratecode having count(ratecode) > 1)
   and rowid not in
       (select min(rowid) from xprice_srchannelrate group by ratecode having count(ratecode) > 1)

delete from xprice_srchannelrate t1
where rowid <> (select min(rowid) from xprice_srchannelrate t2 where t1.ratecode = t2.ratecode);
select * from xprice_srchannelrate;


sql一般函数:length,substr,concat:

select engname from param_ccy t, xprice_ctchannelparam x where t.ccyid = substr(x.ratecode, 0, length(x.ratecode) - 3);


oracle多表连接时更新数据库:

update  xprice_ctchannelparam a
set a.ratename = concat(a.ratename, (select b.engname from param_ccy b where b.ccyid = substr(a.ratecode, length(a.ratecode) - 3, length(a.ratecode) ) ) )
where exists (select b.engname from param_ccy b where b.ccyid = substr(a.ratecode, length(a.ratecode) - 3, length(a.ratecode)) )


下面为网上转载:

 1.INSERT INTO SELECT语句

      语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1

      要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下:


   --1.创建测试表
    create TABLE Table1
    (
        a 
varchar(10),
        b 
varchar(10),
        c 
varchar(10),
        
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
        (
            a 
ASC
        )
    ) 
ON [PRIMARY]

    
create TABLE Table2
    (
        a 
varchar(10),
        c 
varchar(10),
        d 
int,
        
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
        (
            a 
ASC
        )
    ) 
ON [PRIMARY]
    
GO
    
--2.创建测试数据
    Insert into Table1 values('','asds','90')
    
Insert into Table1 values('','asds','100')
    
Insert into Table1 values('','asds','80')
    
Insert into Table1 values('','asds',null)
    
GO
    
select * from Table2

    
--3.INSERT INTO SELECT语句复制表数据
    Insert into Table2(a, c, d) select a,c,5 from Table1
    
GO

    
--4.显示更新后的结果
    select * from Table2
    
GO
    
--5.删除测试表
    drop TABLE Table1
    
drop TABLE Table2


      2.SELECT INTO FROM语句

      语句形式为:SELECT vale1, value2 into Table2 from Table1

      要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:


   --1.创建测试表
    create TABLE Table1
    (
        a 
varchar(10),
        b 
varchar(10),
        c 
varchar(10),
        
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
        (
            a 
ASC
        )
    ) 
ON [PRIMARY]
    
GO

    
--2.创建测试数据
    Insert into Table1 values('','asds','90')
    
Insert into Table1 values('','asds','100')
    
Insert into Table1 values('','asds','80')
    
Insert into Table1 values('','asds',null)
    
GO

    
--3.SELECT INTO FROM语句创建表Table2并复制数据
    select a,c INTO Table2 from Table1
    
GO

    
--4.显示更新后的结果
    select * from Table2
    
GO
    
--5.删除测试表
    drop TABLE Table1
    
drop TABLE Table2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值