SQL关于两表关联的update

本文介绍了在Oracle数据库中实现两表关联更新的具体方法,并提供了实际使用的SQL语句示例。文章通过实例展示了如何将一个表中的city_name和customer_type字段更新为另一个表中的对应值。

关于两表关联的update,但语句怎么写都不正确,老是报错,于是心惊肉跳(就怕不能及时完成操作)去查了一下,NND,原来把SQL写成了在SQL Server下面的特有形式,这种语法在Oracle下面是行不通的,急忙改回来,及时完成了任务。顺便也把查到的SQL帖出来,哪天再忘记了,也好在这里找回来:

   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
                 )

   -- update 超过2个值
   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
                 )

-

资料引用:file:///F:/学习类资料/索迪作业/SSH/SSHReferences/两表(多表)关联update的写法_www_knowsky_com.htm

### SQL Update Join Two Tables Syntax and Examples When performing updates across multiple tables using joins, it's important to understand the limitations of certain types of queries. For instance, when an `UPDATE` statement uses a subquery to modify a single table, the optimizer does not employ semijoin or materialization subquery optimizations[^1]. To address this issue, one can rewrite these operations as multi-table `UPDATE` statements utilizing joins instead. For executing cross-table updates effectively in MySQL, both `INNER JOIN` and `LEFT JOIN` are commonly used within the context of an `UPDATE ... JOIN` operation[^2]. #### Using INNER JOIN for Updates An example where only matching records between two tables get updated: ```sql UPDATE employees AS e INNER JOIN departments AS d ON e.department_id = d.id SET e.salary = e.salary * 1.05, e.last_updated = NOW() WHERE d.name = 'Sales'; ``` This query increases salaries by 5% for all employees working under the Sales department while updating the last modified timestamp accordingly. #### Utilizing LEFT JOIN for Updates There might be scenarios like increasing salary for new hires whose performance metrics aren't yet recorded in another related table (e.g., merittable). In such cases, employing a `LEFT JOIN` ensures that even unmatched rows from the primary table (`employees`) receive necessary modifications without being excluded due to lack of corresponding entries elsewhere[^4]: ```sql UPDATE employees AS e LEFT JOIN merit AS m ON e.employee_id = m.emp_id SET e.salary = CASE WHEN m.rating IS NULL THEN e.salary + 500 ELSE e.salary END; ``` Here, every employee gets either no change or receives additional $500 if there isn’t any associated entry found inside the `merit` table regarding their evaluation scores. #### General Guidelines - Always ensure proper indexing on joined columns. - Test changes thoroughly before applying them widely. - Consider transaction management depending upon database requirements.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值