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.
views:
19answers:
2
+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
2010-08-31 10:21:31
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
2010-08-31 10:22:27
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
2010-08-31 10:44:15
i update the query, what you get wrong?
Haim Evgi
2010-08-31 10:50:42