这是两个表:
表格1
cm_id cost
1 6.52
2 16.52
3 2.12
4 7.14
5 19.09
6 11.52
7 0.12
Run Code Online (Sandbox Code Playgroud)
表2
um_id order_num name
1 517 tommy
2 518 bobby
3 519 scotty
4 520 faris
5 521 justine
6 522 sadie
7 523 nicole
Run Code Online (Sandbox Code Playgroud)
cm_id和um_id代表相同的东西,因此成本可以与每个订单号相关联,即
SELECT table1.cm_id, table1.cost, table2.order_num, table2.order_num
FROM table1, table2
WHERE table1.cm_id=table2.um_id;
Run Code Online (Sandbox Code Playgroud)
我可以使用哪一个SQL语句来删除table1中的行,其中table2中的order_num介于518和520之间?
Zoh*_*aib 17
delete
from table1
where cm_id IN (select um_id from table2 where order_num between 518 and 520)
Run Code Online (Sandbox Code Playgroud)
DELETE table1
FROM table1 INNER JOIN table2 ON table1.cm_id = table2.um_id
AND (table2.order_num BETWEEN 518 AND 520)
--OR
DELETE
FROM table1
USING table1 INNER JOIN table2 ON table1.cm_id = table2.um_id
WHERE (table2.order_num BETWEEN 518 AND 520)
Run Code Online (Sandbox Code Playgroud)
FROM根据Andriy M评论,重复和查询已更改.
小智 5
我更喜欢这种方式
delete from table1
using table1, table2
where table1.cm_id = table2.um_id
and table2.order_num >= 518
and table2.order_num <= 520;
Run Code Online (Sandbox Code Playgroud)