多表关联的update语句、将in中的查询条件按顺序输出和SQL语句增加列、修改列、删除列

时间:2019-08-29
本文章向大家介绍多表关联的update语句、将in中的查询条件按顺序输出和SQL语句增加列、修改列、删除列,主要包括多表关联的update语句、将in中的查询条件按顺序输出和SQL语句增加列、修改列、删除列使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

以下都是oracle的编写方式,其余数据库在网上很容易找到,就不写了。

1、将in中的查询条件按顺序输出,可以使用order by decode 有语句,加条件的id顺序

举例:

  select * from serv_t where serv_id in (957194,965707,960028,6014325) order by decode (serv_id,957194,1,965707,2,960028,3,6014325,4)

2、多表关联的update,修改一个字段,使用exists关键字

  1)直接给set字段值

    update serv_t a set a.product_id='222' where exists (select * from cust_t b where a.cust_id=b.cust_id and a.serv_id=957194)

  2)被修改的值由另一个表运算而来

    update customers a  set   city_name=(select b.city_name from tmp_cust_city b where b.customer_id=a.customer_id)       

    where  exists (select 1 from   tmp_cust_city b where  b.customer_id=a.customer_id)

3、多表关联的update,修改两个字段(以上),被修改的值由另一个表运算而来

  1)update customers a  set  (city_name,customer_type)=(select b.city_name,b.customer_type from  tmp_cust_city b 

     where  b.customer_id=a.customer_id) where  exists (select 1 from tmp_cust_city b where  b.customer_id=a.customer_id)

4、SQL语句增加列、修改列、删除列

  1)增加列:alter table tableName add columnName varchar(30)

  2)修改列类型:alter table tableName alter column columnName varchar(4000)

  3)修改列的名称:EXEC sp_rename 'tableName.column1' , 'column2' ;#(把表名为tableName的column1列名修改为column2)

  4)删除列:alter table tableName drop column columnName

原文地址:https://www.cnblogs.com/huangguabushihaogua/p/11427961.html