tags:

views:

19

answers:

2

Hi, Is there any way to remove the matching rows from MySQL join Query. Actually I have two tables where I have store the pub_id, and post_id in both tables these are common. I want a result when I query all the matching rows from table1 and table2 should not be listed and the non-matching rows should be listed only.

+1  A: 

Query return rows which exists only in one of two tables:

SELECT *
FROM Table1 t1
WHERE NOT EXISTS (Select 1 from Table2 t2 
                 Where t1.pub_id = t2.pub_id
                AND t1.post_Id = t2.post_id)
UNION ALL
SELECT *
FROM Table2 t1
WHERE NOT EXISTS (Select 1 from Table1 t2 
                 Where t1.pub_id = t2.pub_id
                AND t1.post_Id = t2.post_id)
Michael Pakhantsov
A: 

you need something like that:

    SELECT * FROM tablea AS a
   RIGHT  JOIN tableb AS o ON a.id = o.id WHERE a.pub_id IS NULL and a.post_id is null 
UNION    
SELECT * FROM tablea AS a
   LEFT  JOIN tableb AS o ON a.id = o.id WHERE o.pub_id IS NULL and o.post_id is null 
Haim Evgi
I run this query but it did not return exact resultSELECT ID as postlistFROM wp_posts as postaWHERE NOT EXISTS ( SELECT * FROM wp_publication_posts as pub WHERE pub.publication_id=posta.ID and pub.post_id=posta.ID)
junjua
i update the query, what you get wrong?
Haim Evgi