【问题标题】:MSQL get first 100 rows of not intersectionSQL获取前100行不相交
【发布时间】:2020-02-11 12:38:54
【问题描述】:

我在 MSQL 中有 2 个表

第一个名为 tbl_file,它具有以下属性:

  • 身份证
  • hash_id
  • 说明
  • 哑剧类型
  • 创建日期
  • 等等..

第二个叫做tbl_sys_filecontent,它只有:

  • 身份证
  • 内容(base64)

假设这两个表必须具有相同的行大小。 (tbl_filehash-id中的id相同tbl_sys_filecontent

但是,出了点问题,现在如果我们执行:

select count(*) from tbl_sys_filecontent;

我们得到大约 1 000 000 行

我们应该在执行查询后得到相同的行数:

select count(*) from tbl_file as f
JOIN tbl_sys_filecontent as sf on f.hash_id = sf.id;

但是我们只返回了大约 100 000 个结果

我们将第一个查询集合称为 A 和第二个 B

问题是我如何获得等于 A-B 的前 100 行(所以哈希值不相等?)

提前致谢。

【问题讨论】:

    标签: sql sql-server join select


    【解决方案1】:

    不存在

    select *
    from tbl_file as f
    where not exists (select 'x' from tbl_sys_filecontent  as sf where f.hash_id = sf.id);
    

    不在

    select *
    from tbl_file as f
    where f.hash_id not in (select 'x' from tbl_sys_filecontent  as sf);
    

    然而,not existsnot in 更受青睐:https://stackoverflow.com/a/11074428/59119

    左连接

    select f.* from tbl_file as f
        LEFT JOIN tbl_sys_filecontent as sf on f.hash_id = sf.id
    where sf.id is null;
    

    【讨论】:

    • 嗯。对不起,也许没有正确地问问题。我需要从表 tbl_sys_filecontent 中获取 B 查询中不存在的行
    • 所以,我应该得到大约 900 000 行
    • @Joe Doe 以我的例子为例,但换表。应该这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多