Update one table with the data in another table
2014-03-20 08:31
381 查看
This kind of query is called a correlated sub query. For your requirement, the query would be as below....
update students s set s.department = ( select e.qualification from employee e where s.uniquename = e.uniquename and s.Homecountry = e.Homecountry );---create table and insert statements
create table student( name varchar2(20), age number, department varchar2(3), HomeCountry varchar2(10) ); Table created. create table employee5( name varchar2(20), exp number, qualification varchar2(3), homecountry varchar2(10) ); Table created. insert into student values ('Mohan',25,'EEE','India'); insert into student values ('Raja',27,'EEE','India'); insert into student values ('Ahamed',26,'ECE','UK'); insert into student values ('Gokul',25,'IT','USA'); commit; insert into employee5 values ('Mohan',25,'ECE','India'); insert into employee5 values ('Raja',24,'IT','India'); insert into employee5 values ('Palani',26,'ECE','USA'); insert into employee5 values ('Sathesh',29,'CSE','CANADA'); insert into employee5 values ('Ahamed',28,'ECE','UK'); insert into employee5 values ('Gokul',29,'EEE','USA'); commit;Before updating the data...
SQL> select * from student; NAME AGE DEP HOMECOUNTR -------------------- ---------- --- ---------- Mohan 25 EEE India Raja 27 EEE India Ahamed 26 ECE UK Gokul 25 IT USA SQL> select * from employee5; NAME EXP QUA HOMECOUNTR -------------------- ---------- --- ---------- Mohan 25 ECE India Raja 24 IT India Palani 26 ECE USA Sathesh 29 CSE CANADA Ahamed 28 ECE UK Gokul 29 EEE USAUpdate statement and results
1 update student s set s.age = 2 ( select e.exp 3 from employee5 e 4 where e.name = s.name 5 and e.homecountry = s.homecountry 6* ) SQL> / 4 rows updated.
SQL> select * from student; NAME AGE DEP HOMECOUNTR -------------------- ---------- --- ---------- Mohan 25 EEE India Raja 24 EEE India Ahamed 28 ECE UK Gokul 29 IT USA SQL> commit; Commit complete.
update student s set s.age = (select e.exp from employee5 e where e.name = s.name and e.homecountry = s.homecountry and rownum < 2 ) where s.age in (select age from employee5)It wont show the message subquery returns more than one record
相关文章推荐
- Update one table with the data in another table
- Update data in one table with data from another table
- msyql同步的时候报错 : 错误代码: 1293 Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
- update records in one table based on values in another table
- Refreshing One Column based on the value of Another Column in ADFdi Table
- "The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods i"解决方法
- 使用CocoaPods遇到一个问题:The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update ....
- Only one expression can be specified in the select list when the subquery is not introduced with EXI
- [D3] Animate with the General Update Pattern in D3 v4
- A file with the correct name was found in this directory, but it is not the correct one.
- U盘安装系统 (Windows 7篇)(HP) Windows could not update registy data in the installation
- Insert Data From One Table to Another
- 82.Examine the data in the CUST_NAME column of the CUSTOMERS table.
- The CREATE DATA statement with built-in generic types
- 127.Examine the data in the CUSTOMERS table:
- Error with hilo in NHibernate - “could not read a hi value - you need to populate the table”
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- Fixing the "There is already an open DataReader associated with this Command which must be closed first." exception in Entity Framework
- Compare Data from the Same Table in two Different Environments
- DataStage job monitor fails with out of memory error in the javacore file