sql - Delete Rows in a table Based on column value in third table -


i have 3 tables table1, table2 , table3 , following query deletes rows in table1

delete table1  exists (select (1) table2  table1.col1=table2.col1  ,   table1.col2=table2.col2  ,   table1.col3=(select **table3.col3 table3** inner join table2 on table3.col1=table2.col1) 

is query correct? if not, how use third table inside condition?

edit : also, please explain how rewrite query if want delete rows table2 joined table3?

here's 1 way it:

delete table1  (col1, col2, col3) in (   select t1.col1, t1.col2, t1.col3   table1 t1     join table2 t2 on t1.col1 = t2.col1 , t1.col2 = t2.col2     join table3 t3 on t1.col3 = t3.col3 , t2.col1 = t3.col1   ); 

or using exists might faster:

delete table1  exists (   select *    table2     join table3 on table2.col1 = table3.col1   table1.col3 = table3.col3 ,      table1.col1 = table2.col1 ,      table1.col2 = table2.col2); 

Comments

Popular posts from this blog

c++ - CryptStringToBinary API behavior -

c++ - Correct method for redrawing a layered window -

java.util.scanner - How to read and add only numbers to array from a text file -