0

I have two tables like this:

Table L   
 A | B  
 C | D

Table K     
 A | B   
 C | D  
 E | F
etc etc

I whish to delete in table K all the lines that exist in Table L.

However when I do delete it will only eliminate the first line. I would like that if more lines where inserted into Table L that coincide with lines in Table K the delete would work as well, this is, not having to do a "manual" delete

Thanks in advance

1
  • 2
    mysql or sqlite? remove the wrong tag Commented Oct 1, 2016 at 18:31

2 Answers 2

1

You can use delete with an alias and an existential quantifier to accomplish that:

delete k
from K as k
where exists (
    select * from L as l
    where l.col1=k.col1 and l.col2=k.col2
)
Sign up to request clarification or add additional context in comments.

2 Comments

@SOaddict Right. Thank you!
Thanks, my query was mostly right except I had an = after the where clause. that helped!
0
DELETE  FROM K 
WHERE ( Col1,Col2 ) 
  NOT IN 
( 
  SELECT k1.Col1, k1.Col2
    FROM K k1
    LEFT OUTER JOIN L l1
      ON k1.Col1 = l1.Col1
     AND k1.Col2 = l1.Col2
    WHERE l1.Col1 IS NULL OR l1.Col2 IS NULL
); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.