2023年8月3日发(作者:)
ORACLE多表关联UPDATE语句1) 最简单的形式SQL 代码 --经确认customers表中所有customer_id⼩于1000均为'北京'--1000以内的均是公司⾛向全国之前的本城市的⽼客户:)update customersset city_name='北京'where customer_id<1000
2) 两表(多表)关联update -- 仅在where字句中的连接SQL 代码--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别update customers a -- 使⽤别名set customer_type='01' --01 为vip,00为普通where exists (select 1from tmp_cust_city bwhere er_id=er_id)
3) 两表(多表)关联update -- 被修改值由另⼀个表运算⽽来SQL 代码update customers a -- 使⽤别名set city_name=(select _name from tmp_cust_city b where er_id=er_id)where exists (select 1from tmp_cust_city bwhere er_id=er_id)-- update 超过2个值update customers a -- 使⽤别名set (city_name,customer_type)=(select _name,er_typefrom tmp_cust_city bwhere er_id=er_id)where exists (select 1from tmp_cust_city bwhere er_id=er_id)-- ⽅法
表2SET 表2.C = (SELECT
B FROM
表1 WHERE
表1.A = 表2.A)WHERE EXISTS ( SELECT
1 FROM
表1 WHERE
表1.A = 表2.A)
-- ⽅法2MERGE INTO
表2
USING 表1ON
( 表2.A = 表1.A ) -- 条件是 A 相同WHEN
MATCHED THEN
UPDATE
SET
表2.C = 表1.B -- 匹配的时候,更新
⼆,oracle随机读取表中的N条数据⽅法:1) select
* from
(select
* from
tablename order
by
sys_guid()) where
rownum < N;
2) select
* from
(select
* from
tablename order
by
dbms_) where
rownum< N;
3) select
* from
(select
* from
table_name sample(10) order
by
trunc(dbms_(0, 1000))) where
rownum < N;
说明:
sample(10)含义为中的10%数据,sample值应该在[0.000001,99.999999]之间,其中 sys_guid() 和 dbms_都是内部函数注: 在使1)⽅法时,即使⽤sys_guid() 这种⽅法时,有时会获取到相同的记录,即:和前⼀次查询的结果集是⼀样的(可能是和有关:windows正常,linux异常;也可能是因为sys_guid()函数本⾝的问题,有待继续研究) 所以,为确保在不同的平台每次读取的数据都是随机的,建议采⽤2)和3)两种⽅案,其中2)⽅案更常⽤。3)⽅案缩⼩了查询的范围,在查询⼤表,且要提取数据不是很不多的情况下,会对查询速度上有⼀定的提⾼
2023年8月3日发(作者:)
ORACLE多表关联UPDATE语句1) 最简单的形式SQL 代码 --经确认customers表中所有customer_id⼩于1000均为'北京'--1000以内的均是公司⾛向全国之前的本城市的⽼客户:)update customersset city_name='北京'where customer_id<1000
2) 两表(多表)关联update -- 仅在where字句中的连接SQL 代码--这次提取的数据都是VIP,且包括新增的,所以顺便更新客户类别update customers a -- 使⽤别名set customer_type='01' --01 为vip,00为普通where exists (select 1from tmp_cust_city bwhere er_id=er_id)
3) 两表(多表)关联update -- 被修改值由另⼀个表运算⽽来SQL 代码update customers a -- 使⽤别名set city_name=(select _name from tmp_cust_city b where er_id=er_id)where exists (select 1from tmp_cust_city bwhere er_id=er_id)-- update 超过2个值update customers a -- 使⽤别名set (city_name,customer_type)=(select _name,er_typefrom tmp_cust_city bwhere er_id=er_id)where exists (select 1from tmp_cust_city bwhere er_id=er_id)-- ⽅法
表2SET 表2.C = (SELECT
B FROM
表1 WHERE
表1.A = 表2.A)WHERE EXISTS ( SELECT
1 FROM
表1 WHERE
表1.A = 表2.A)
-- ⽅法2MERGE INTO
表2
USING 表1ON
( 表2.A = 表1.A ) -- 条件是 A 相同WHEN
MATCHED THEN
UPDATE
SET
表2.C = 表1.B -- 匹配的时候,更新
⼆,oracle随机读取表中的N条数据⽅法:1) select
* from
(select
* from
tablename order
by
sys_guid()) where
rownum < N;
2) select
* from
(select
* from
tablename order
by
dbms_) where
rownum< N;
3) select
* from
(select
* from
table_name sample(10) order
by
trunc(dbms_(0, 1000))) where
rownum < N;
说明:
sample(10)含义为中的10%数据,sample值应该在[0.000001,99.999999]之间,其中 sys_guid() 和 dbms_都是内部函数注: 在使1)⽅法时,即使⽤sys_guid() 这种⽅法时,有时会获取到相同的记录,即:和前⼀次查询的结果集是⼀样的(可能是和有关:windows正常,linux异常;也可能是因为sys_guid()函数本⾝的问题,有待继续研究) 所以,为确保在不同的平台每次读取的数据都是随机的,建议采⽤2)和3)两种⽅案,其中2)⽅案更常⽤。3)⽅案缩⼩了查询的范围,在查询⼤表,且要提取数据不是很不多的情况下,会对查询速度上有⼀定的提⾼
发布评论