【问题标题】:Count of unmatched records between two Tables in PostgreSQLPostgreSQL 中两个表之间不匹配记录的计数
【发布时间】:2018-01-09 22:00:28
【问题描述】:

如何获取PostgreSQL中两个表之间不匹配记录的计数?

Create Table t1
(
 id integer NOT NULL,
 name text,
 address text
 );

Create Table t2
(
 name text,
 contact varchar(12)
 );

 insert into t1 values(1, 'A INc',  '29 Ave. Toronto, ON');
 insert into t1 values(2,  'B INc', '115 Street New York, NY');

 insert into t2 values('A Inc',  '123-456-7890');
 insert into t2 values('C INc', '234-567-8901');

如何通过Query获取不匹配记录的行数

预期输出: 计数 1 结果的输出必须是 Count :1

【问题讨论】:

    标签: sql database postgresql join


    【解决方案1】:

    这应该可行:

    select count(*) from t1 where name not in(select name from t2 )
    

    【讨论】:

      【解决方案2】:

      使用外连接查找不属于集合的行。

      select count(*)
      from t1
      right join t2
      on t1.name = t2.name
      where t1.name is null;
      

      【讨论】:

        猜你喜欢
        • 2022-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多