Recipe 4.10. Updating with Values from Another Table
Problem
select *
from new_sal
DEPTNO SAL
------ ----------
10 4000
Column DEPTNO is the primary key of table NEW_SAL. You want to update the salaries and commission of certain employees in table EMP using values table NEW_SAL if there is a match between EMP.DEPTNO and NEW_SAL.DEPTNO, update EMP.SAL to NEW_SAL.SAL, and update EMP.COMM to 50% of NEW_SAL.SAL. The rows in EMP are as follows:
select deptno,ename,sal,comm from emp order by 1 DEPTNO ENAME SAL COMM ------ ---------- ---------- ---------- 10 CLARK 2450 10 KING 5000 10 MILLER 1300 20 SMITH 800 20 ADAMS 1100 20 FORD 3000 20 SCOTT 3000 20 JONES 2975 30 ALLEN 1600 300 30 BLAKE 2850 30 MARTIN 1250 1400 30 JAMES 950 30 TURNER 1500 0 30 WARD 1250 500
Solution
1 update emp e set (e.sal,e.comm) = (select ns.sal, ns.sal/2 2 from new_sal ns 3 where ns.deptno=e.deptno) 4 where exists ( select null 5 from new_sal ns 6 where ns.deptno = e.deptno )
------------------
1 update ( 2 select e.sal as emp_sal, e.comm as emp_comm, 3 ns.sal as ns_sal, ns.sal/2 as ns_comm 4 from emp e, new_sal ns 5 where e.deptno = ns.deptno 6 ) set emp_sal = ns_sal, emp_comm = ns_comm
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23895263/viewspace-681076/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Recipe 6.10. Creating a Delimited List from Table RowsMIT
- Recipe 3.1. Stacking One Rowset atop Another
- Recipe 6.7. Extracting Initials from a Name
- Recipe 5.2. Listing a Table's Columns
- how to move a MediaWiki wiki from one server to anotherServer
- How to move ASM database files from one diskgroup to anotherASMDatabase
- [轉]Updating Session Variables from Dashboards using Presentation VariablesSession
- 023 Given an integer n. get the number of 0, 2, 4 from all the values from [0, n]
- [Most.js] Create Streams From Single Values With Most.jsJS
- ORA-16649: possible failover to another database prevents this database from beiAIDatabase
- iOS開發之SQLite–C語言介面規範(四) :Result Values From A QueryiOSSQLiteC語言
- GoldenGate "Error mapping from table.a to table.a"錯誤測試GoErrorAPP
- Django中values()和values_list()Django
- You can‘t specify target table ‘Person‘ for update in FROM clause
- Oracle模擬MySQL的show index from table命令OracleMySqlIndex
- 關於 mysql 中的 select * from table_a,table_b 的問題MySql
- mysql中You can’t specify target table for update in FROM clMySql
- Error: cannot fetch last explain plan from PLAN_TABLEErrorASTAI
- 5.編寫recipe
- MysqL中的Show Index From Table_Name命令說明MySqlIndex
- Script: To remove Chained Rows from a Table (Doc ID 1019556.6)REMAI
- Subarray Distinct Values
- 高效的SQL(index values與index column values關係?)SQLIndex
- Yet Another Problem
- Bug 9369183 - ORA-32349 creating MView on prebuilt table using select from remote tableViewUIREM
- JavaScript 陣列values()JavaScript陣列
- JavaScript Object.values()JavaScriptObject
- master..spt_valuesAST
- Deployer recipe中Laravel配置檔案Laravel
- from v * ERROR at line 1: ORA-00942: table or view does not existErrorView
- Harden the Hacker Thinking (Updating)Thinking
- Another Intro for CookiesCookie
- stylus , another css processorCSS
- Another article published by apiAPI
- pipe stderr into another process
- Recipe 4.11. Merging Records
- Recipe 5.6. Using SQL to Generate SQLSQL
- Android面試總結(updating)Android面試