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
ZPPP
V2EX  ›  MySQL

请教一条 SQL 写法

  •  
  •   ZPPP · 2020-02-18 16:08:37 +08:00 · 3681 次点击
    这是一个创建于 1500 天前的主题,其中的信息可能已经有所发展或是发生改变。
    表结构如下:
    id name
    1 A
    2 A
    2 B
    3 A
    3 B
    3 C
    查询 name 仅含 A,B,得到结果 id 为 2。
    第 1 条附言  ·  2020-02-18 16:39:03 +08:00
    表的数量级为千万 jibie
    15 条回复    2020-02-19 10:28:29 +08:00
    oneisall8955
        1
    oneisall8955  
       2020-02-18 16:13:15 +08:00 via Android
    。。。
    execute
        2
    execute  
       2020-02-18 16:18:54 +08:00
    没有附加条件么?
    那 :where id = 2
    shyrock
        3
    shyrock  
       2020-02-18 16:19:37 +08:00
    这。。。作业?好奇 lz 咋写的 SQL
    speedofstephen
        4
    speedofstephen  
       2020-02-18 16:21:54 +08:00   ❤️ 1
    select H1.id from
    (select id, count(name) as cnt from T where name in ('A' ,'B') group by id) H1
    inner join
    (select id, count(name) as cnt from T group by id) H2
    where H1.id = H2.id and H1.cnt = H2.cnt;
    speedofstephen
        5
    speedofstephen  
       2020-02-18 16:23:19 +08:00
    错了 好像还得加上 where H1.cnt=2
    shyrock
        6
    shyrock  
       2020-02-18 16:24:22 +08:00
    select distinct(id) from T MINUS select distinct(id) from T where name in ('C','D',...)
    ZPPP
        7
    ZPPP  
    OP
       2020-02-18 16:44:23 +08:00
    @shyrock 表中不存在 C 和 D。
    @speedofstephen 表中含有千万条数据,内连接 H2 会比较慢。
    taotaodaddy
        8
    taotaodaddy  
       2020-02-18 17:14:11 +08:00 via Android
    考虑一下 concat ?效率不知
    ebony0319
        9
    ebony0319  
       2020-02-18 17:15:18 +08:00
    select * from table t where name in ('A','B') and 2=(
    select count(1) from table t1 where t1.name=t.name
    )
    这里的 2 为('A','B') 的长度。
    ebony0319
        10
    ebony0319  
       2020-02-18 17:16:06 +08:00
    修改一下:
    select * from table t where name in ('A','B') and 2=(
    select count(1) from table t1 where t1.id=t.id
    )
    这里的 2 为('A','B') 的长度。
    ZPPP
        11
    ZPPP  
    OP
       2020-02-18 21:23:53 +08:00 via Android
    @ebony0319 有个 id 对应 A 和 C 也会返回
    ZPPP
        12
    ZPPP  
    OP
       2020-02-18 22:37:50 +08:00
    @speedofstephen 感谢,是正解。
    turan12
        13
    turan12  
       2020-02-19 00:20:28 +08:00
    请楼主独立完成作业。。。
    zhuzhibin
        14
    zhuzhibin  
       2020-02-19 01:21:21 +08:00 via iPhone
    内联取交集?
    alya
        15
    alya  
       2020-02-19 10:28:29 +08:00
    select
    id
    from t1
    group by id
    having collect_set(name) in (array("A","B"), array("B", "A"))
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3320 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 13:34 · PVG 21:34 · LAX 06:34 · JFK 09:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.