Calculate the difference between results of two count(*) queries based on 2 tables in PostgreSQL

后端 未结 5 1332
孤城傲影
孤城傲影 2020-12-16 10:29

I want to calculate the difference between the results of 2 count(*)-type SELECT queries executed on 2 separate tables of my PostgreSQL database.

This w

5条回答
  •  离开以前
    2020-12-16 10:56

    Another option is to use SUM:

    SELECT SUM(val) AS result
    FROM (SELECT count(*) AS val FROM tab2
          UNION ALL
          SELECT -count(*) FROM tab1) sub;
    

    db<>fiddle demo

提交回复
热议问题