mysql - Count all records that does not exist to other table - SQL Query -
i have two(2) tables , i'm trying count records table1 , table1_delta pagename table1_delta not yet listed table1. incase pagename table1_delta listed table1, status must 1 included in count result.
sample table structure:
table1
+-----------+--------+ | pagename | status | +-----------+--------+ | pagename1 | 2 | | pagename2 | 1 | +-----------+--------+
table1_delta
+-----------+ | pagename | +-----------+ | pagename1 | | pagename2 | | pagename3 | | pagename4 | +-----------+
the table sample should return "3".
pagename3 , pagename4 not listed in table1(that returns 2) , pagename2 table1 has status = 1(that returns 1). in total there 3 pagenames table1_delta not listed in table1 , record table1 status = 1. i'm wondering on how query of this? i'm using mysql v5.6.17. thanks!
here alternative solution using joins:
select count(*) table1_delta t1 left join table1 t2 on t1.pagename = t2.pagename t2.status null or t2.status = 1
here temporary table above query looks like:
+-----------+--------+ | pagename | status | +-----------+--------+ | pagename1 | 2 | # row not counted | pagename2 | 1 | # +1 row has status = 1 , counted | pagename3 | null | # +1 row has status = null , counted | pagename4 | null | # +1 row null , counted +-----------+--------+
check out link below running demo.
Comments
Post a Comment