[英]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.