› MySQL 5.5 Community Server
› MySQL 5.6 Community Server
› Percona Configuration Wizard
› XtraBackup 搭建主从复制
Great Sites on MySQL
› Percona
› MySQL Performance Blog
› Severalnines
推荐管理工具
› Sequel Pro
› phpMyAdmin
推荐书目
› MySQL Cookbook
MySQL 相关项目
› MariaDB
› Drizzle
参考文档
› http://mysql-python.sourceforge.net/MySQLdb.html
jahan
V2EX  ›  MySQL

没有 except 的 mysql,实现 except 的途径分析

  •  
  •   jahan · Mar 29, 2018 · 9121 views
    This topic created in 3103 days ago, the information mentioned may be changed or developed.

    select * from (

    select count(*) as cnt ,all columns ( select distinct * from table1
    union all
    select distinct * from table2 ) group by all columns ) where cnt =1

    想把在在 db2 中,要想知道在 table1 而不再 table2 中的数据,可以 select * from table1 except select * from table2 可是现在在 mysql 下面 except 没有,用这个 select * from (

    select  count(*) as cnt  ,all columns 
    (
    select distinct * from table1    
     union all  
    select distinct * from table2
    )
    group by all columns 
    ) where cnt =1 
    

    一直报错,也没查到 all columns 的用法。

    10 replies  •  2018-03-29 20:32:54 +08:00
    b821025551b
        1
    b821025551b  
       Mar 29, 2018
    用 table1 left join table2 就行了。
    jahan
        2
    jahan  
    OP
       Mar 29, 2018
    @b821025551b 主要是没有一个字段可以把两个记录区分开,只有全字段内容相同的列才不要了。

    left jion 都要有个 on,这个 on 的话,就没办法写了。
    jahan
        3
    jahan  
    OP
       Mar 29, 2018
    @b821025551b 这 2 表没有 pk,只是一堆字符串和数字。让我 on 的无从下手
    b821025551b
        4
    b821025551b  
       Mar 29, 2018
    test1:
    id | value
    1 |A
    2 |B
    3 |C

    test2:
    id | value
    1 |A
    2 |B
    3 |D

    select test1.* from test1 left join test2 on test1.value=test2.value where test2.value is null

    result:
    id | value
    3 |C

    和 pk 没关系吧,on 在你要比对的字段上并且 where 限定右表那个字段是 null 就可以了。
    jahan
        5
    jahan  
    OP
       Mar 29, 2018
    那要全字段比较了,我试试
    jahan
        6
    jahan  
    OP
       Mar 29, 2018
    select * from (
    select count(*) as cnt ,id,value
    (
    select distinct * from table1
    union all
    select distinct * from table2
    )
    group by id,value
    ) where cnt =1

    这种就一直报错,想不出哪里有问题
    wqzjk393
        7
    wqzjk393  
       Mar 29, 2018 via iPhone
    @jahan 少个 from …
    jahan
        8
    jahan  
    OP
       Mar 29, 2018
    @wqzjk393 这个有了,一直报每个表都需要别名。可是
    select a.* from (
    select count(*) as cnt ,id,value
    (
    select distinct * from table1 a
    union all
    select distinct * from table2 b
    )
    group by a.id,a.value
    ) where a.cnt =1

    ??
    wqzjk393
        9
    wqzjk393  
       Mar 29, 2018 via iPhone
    from
    (
    select distinct * from table1 a
    union all
    select distinct * from table2 b
    )
    这里,应该是把 union 当成子查询的表了,而根据规定所有子查询的表都要有别名。 另外一点,能不用*查询就别用*,经常出一些奇奇怪怪的错误,而且在工程上基本上老大都不允许这么查
    choulinlin
        10
    choulinlin  
       Mar 29, 2018 via Android
    where not exists ?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   833 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 21:29 · PVG 05:29 · LAX 14:29 · JFK 17:29
    ♥ Do have faith in what you're doing.