ripe
1.32D
V2EX  ›  问与答

一个 SQL 查询的问题

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

    现在有两张表,2018_word_freqt5000.

    两张表有些单词是共有的,我想列出 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
        1
    MOONYANYI  
       Jan 12, 2019   ❤️ 1
    用 inner 查出两张表相同的,再 not in
    yasumoto
        2
    yasumoto  
       Jan 12, 2019   ❤️ 1
    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
        3
    lsongiu  
       Jan 12, 2019   ❤️ 1
    我咋感觉 lz 本身的 sql 也能查出来才对呢。难道有空格?
    thinkif
        4
    thinkif  
       Jan 12, 2019   ❤️ 1
    是我眼花了么?仅从截图里看,2 个表中的词没有看到重复的
    ripe
        5
    ripe  
    OP
       Jan 12, 2019
    @thinkif 第一个表 2000 个单词。第二个表 5000 个单词,然后截图里显示的没有按词频排序,所以没看到重复的
    thinkif
        6
    thinkif  
       Jan 12, 2019
    @Laynooor #5
    not in
    MOONYANYI
        7
    MOONYANYI  
       Jan 12, 2019
    还可以用左外连接,再判断右表字段为 null 的
    oaix
        8
    oaix  
       Jan 12, 2019   ❤️ 1
    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
        9
    thinkif  
       Jan 12, 2019   ❤️ 1
    @Laynooor #5
    一不留神发布去了
    正常来说,not in / not exists / left join + is not null 这几种写法都可以的
    然后如 @lsongiu #3 所说,如果词中有空格干扰也可能出问题,试试 trim 掉空格或换行符之类的试试
    ripe
        10
    ripe  
    OP
       Jan 12, 2019
    @lsongiu 确实是空格的问题,第二个表前面有空格,去掉后就正常了。
    qinrui
        11
    qinrui  
       Jan 12, 2019 via iPhone
    select word from a left outer join b on a.word=b.word where b.word is null
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6099 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 54ms · UTC 02:52 · PVG 10:52 · LAX 19:52 · JFK 22:52
    ♥ Do have faith in what you're doing.