开发者

How to Remove matching rows From mysql Join Result and show non-matching rows?

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开发者_StackOverflow社区 from table1 and table2 should not be listed and the non-matching rows should be listed only.


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)


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 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜