ripe
1.32D
V2EX  ›  问与答

一个 SQL 查询的问题

  •  
  •   ripe · Jan 12, 2019 · 2365 views
    This topic created in 2870 days ago, the information mentioned may be changed or developed.

    现在有两张表,2018_word_freq 和 t5000.

    两张表有些单词是共有的,我想列出 2018_word_freq 特有的单词。(比如两张表都有 the 这个单词,那么最后查询结果中不会出现这个词)

    2018.png

    5000.png

    这是我在 Google Bigquery 上执行的查询语句,但是返回的结果并没有排除重复的项目。

    result.png

    感觉是自己 SQL 没写对,但是网上查了半天,语句改了又改结果还是不正确……

    Supplement 1  ·  Jan 12, 2019

    t5000 前面有空格……去掉之后再查询结果就正常了

    Snipaste_2019-01-12_10-41-05.png

    11 replies  •  2019-01-12 12:22:00 +08:00
    MOONYANYI
    MOONYANYI
    Jan 12, 2019❤️1
    1
    用 inner 查出两张表相同的,再 not in
    yasumoto
    yasumoto
    Jan 12, 2019❤️1
    2
    select wf.* from 2018_word_freq wf where wf.word not in (SELECT word from t5000 where _______word in (wf.word));

    select wf.* from 2018_word_freq wf where wf.word not in (SELECT word from 2018_word_freq twf , t5000 t where twf.word = t._______word );

    跟楼上的思路一样 不知道是否有更有效的查询方法
    lsongiu
    lsongiu
    Jan 12, 2019❤️1
    3
    我咋感觉 lz 本身的 sql 也能查出来才对呢。难道有空格?
    thinkif
    thinkif
    Jan 12, 2019❤️1
    4
    是我眼花了么?仅从截图里看,2 个表中的词没有看到重复的
    ripe
    ripe
    OP
    Jan 12, 2019
    5
    @thinkif 第一个表 2000 个单词。第二个表 5000 个单词,然后截图里显示的没有按词频排序,所以没看到重复的
    thinkif
    thinkif
    Jan 12, 2019
    6
    @Laynooor #5
    not in
    MOONYANYI
    MOONYANYI
    Jan 12, 2019
    7
    还可以用左外连接,再判断右表字段为 null 的
    oaix
    oaix
    Jan 12, 2019❤️1
    8
    select a.* from 2018_word_freq a left join t5000 b on a.word = b.______word where b.______word is null

    如果你的数据库支持 left anti join 可以使用 select a.* from 2018_word_freq a left anti join t5000 b on a.word = b.______word
    thinkif
    thinkif
    Jan 12, 2019❤️1
    9
    @Laynooor #5
    一不留神发布去了
    正常来说,not in / not exists / left join + is not null 这几种写法都可以的
    然后如 @lsongiu #3 所说,如果词中有空格干扰也可能出问题,试试 trim 掉空格或换行符之类的试试
    ripe
    ripe
    OP
    Jan 12, 2019
    10
    @lsongiu 确实是空格的问题,第二个表前面有空格,去掉后就正常了。
    qinrui
    qinrui
    Jan 12, 2019 via iPhone
    11
    select word from a left outer join b on a.word=b.word where b.word is null
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1849 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 11:20 · PVG 19:20 · LAX 03:20 · JFK 06:20
    ♥ Do have faith in what you're doing.