[英]delete multiple rows from different tables on oracle
我有两张桌子。一张是学生,另一张是薪水。
学生桌
id | name | code | status
1 | steven | 123 | 100
2 | joe | 678 | 200
3 | paul | 758 | 100
工资表
id | code | status | currency
1 | 123 | 100 | euro
2 | 678 | 200 | dolar
3 | 758 | 520 | yuan
我想从学生表中删除第 1 行,从工资表中删除第 1 行和第 2 行,因为代码和状态字段是相同的。
我写了那个查询
delete a,b Student as a , join Salary as b
on a.code= b.code and a.status = b.status
但它不起作用。我想用一个查询删除行。你知道吗?
这样的事情会做吗? 但是,PL/SQL 不是 SQL。
初始数据集:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
1 steven 123 100
2 joe 678 200
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
1 123 100 euro
2 678 200 dollar
3 758 520 yuan
删除常见的(CODE, STATUS)
组合:
SQL> begin
2 for cur_r in (select code, status from student
3 intersect
4 select code, status from salary
5 )
6 loop
7 delete from student where code = cur_r.code and status = cur_r.status;
8 delete from salary where code = cur_r.code and status = cur_r.status;
9 end loop;
10 end;
11 /
PL/SQL procedure successfully completed.
结果:
SQL> select * from student;
ID NAME CODE STATUS
---------- ------ ---------- ----------
3 paul 758 100
SQL> select * from salary;
ID CODE STATUS CURREN
---------- ---------- ---------- ------
3 758 520 yuan
SQL>
您可以使用两个语句轻松完成此操作:
delete student s where exists(
select * from student stu inner join salary sal on stu.code=sal.code and stu.status=sal.status and stu.id=s.id);
delete salary sal where not exists (select code from student stu where stu.code=sal.code);
第一个删除两个表中具有相同代码和状态的所有学生,第二个是从工资表中删除学生表中不存在代码的所有行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.