Count all records that does not exist to other table - SQL Query

后端 未结 3 674
醉酒成梦
醉酒成梦 2021-01-19 15:30

I have two(2) tables and I\'m trying to count all records from Table1 and Table1_delta were pagename from Table1_delta is not yet listed into Table1. Incase pagename from Ta

3条回答
  •  猫巷女王i
    2021-01-19 16:06

    Here is an alternative solution using joins:

    SELECT COUNT(*)
    FROM Table1_delta t1 LEFT JOIN Table1 t2
    ON t1.pagename = t2.pagename
    WHERE t2.status IS NULL OR t2.status = 1
    

    Here is what the temporary table from the above query looks like:

    +-----------+--------+
    | pagename  | status |
    +-----------+--------+
    | pagename1 |  2     |    # this row is NOT counted
    | pagename2 |  1     |    # +1 this row has status = 1 and is counted
    | pagename3 |  null  |    # +1 this row has status = null and is counted
    | pagename4 |  null  |    # +1 this row is also null and is counted
    +-----------+--------+
    

    Check out the link below for a running demo.

    SQLFiddle

提交回复
热议问题