Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete records in one table based on the values in another table?

Tags:

sql

mysql

Here are two tables:

table1

cm_id   cost
1       6.52
2       16.52
3       2.12
4       7.14
5       19.09
6       11.52
7       0.12

table2

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

cm_id and um_id represent the same thing so the cost can be tied to each order number, ie

SELECT table1.cm_id, table1.cost, table2.order_num, table2.order_num
FROM table1, table2
WHERE table1.cm_id=table2.um_id;

What is the single SQL statement I can use to delete rows from table1 where the order_num in table2 is between 518 and 520?

like image 472
user784637 Avatar asked Nov 28 '11 05:11

user784637


Video Answer


1 Answers

delete 
from table1
where cm_id IN (select um_id from table2 where order_num between 518 and 520)
like image 183
Zohaib Avatar answered Oct 16 '22 11:10

Zohaib