V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
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 · 2018-03-29 14:46:19 +08:00 · 7783 次点击
    这是一个创建于 2191 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2018-03-29 20:32:54 +08:00
    b821025551b
        1
    b821025551b  
       2018-03-29 15:08:53 +08:00
    用 table1 left join table2 就行了。
    jahan
        2
    jahan  
    OP
       2018-03-29 15:14:53 +08:00
    @b821025551b 主要是没有一个字段可以把两个记录区分开,只有全字段内容相同的列才不要了。

    left jion 都要有个 on,这个 on 的话,就没办法写了。
    jahan
        3
    jahan  
    OP
       2018-03-29 15:17:05 +08:00
    @b821025551b 这 2 表没有 pk,只是一堆字符串和数字。让我 on 的无从下手
    b821025551b
        4
    b821025551b  
       2018-03-29 15:22:30 +08:00
    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
       2018-03-29 16:29:29 +08:00
    那要全字段比较了,我试试
    jahan
        6
    jahan  
    OP
       2018-03-29 16:54:08 +08:00
    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  
       2018-03-29 19:35:00 +08:00 via iPhone
    @jahan 少个 from …
    jahan
        8
    jahan  
    OP
       2018-03-29 19:50:02 +08:00
    @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  
       2018-03-29 20:25:54 +08:00 via iPhone
    from
    (
    select distinct * from table1 a
    union all
    select distinct * from table2 b
    )
    这里,应该是把 union 当成子查询的表了,而根据规定所有子查询的表都要有别名。 另外一点,能不用*查询就别用*,经常出一些奇奇怪怪的错误,而且在工程上基本上老大都不允许这么查
    choulinlin
        10
    choulinlin  
       2018-03-29 20:32:54 +08:00 via Android
    where not exists ?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3491 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 11:01 · PVG 19:01 · LAX 04:01 · JFK 07:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.