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

请教: mysql 同表有两条 service_id 相同数据, flag 不同数据, flag 只有两个值,要求查出值 flag1 存在, flag2 不存在的数据

  •  
  •   ouyc · 2021-01-23 13:23:48 +08:00 · 2066 次点击
    这是一个创建于 1160 天前的主题,其中的信息可能已经有所发展或是发生改变。
    表数据较大,大约一百万条。目前思路是左连接,但会走全表查询,速度很慢

    select
    si1.*
    from
    service_info si1
    left outer join service_info si2 on
    si1.service_id = si2.service_id
    where
    si1.flag= 'flag1'
    and si2 .serial_id is null;

    请问有没有什么更快的解决方法或思路?目前仅使用 sql 查询,不做其他例如分区表或分表操作。
    10 条回复    2021-01-23 22:07:47 +08:00
    learningman
        1
    learningman  
       2021-01-23 13:42:53 +08:00
    我的思路是读出来然后全部异或(
    前提是 service_id 是个数字
    ouyc
        2
    ouyc  
    OP
       2021-01-23 14:10:31 +08:00
    service_id 可以是一个数字。是否还需要连表查询来,再异或。
    7Qi7Qi
        3
    7Qi7Qi  
       2021-01-23 15:27:57 +08:00
    先查出来两条 service_id 相同的数据 service_id 集合,再直接判断 flag 值?
    nuistzhou
        4
    nuistzhou  
       2021-01-23 17:10:41 +08:00 via iPhone
    group by service_id having ?
    danielmiao
        5
    danielmiao  
       2021-01-23 17:30:44 +08:00
    如果我没理解错的话:
    1. 数据表中有 service_id 相同的数据,但有些 service_id 只有一条;
    2. flag 只有 flag1 和 flag2,不存在空的情况;

    我的思路是先吧 flag2 的数据查出来,然后去表里查 flag1 且在 flag2 里有的数据。

    SELECT * FROM test3 WHERE flag = 'flag1' AND sid IN ( SELECT sid FROM test3 WHERE flag='flag2' );
    zlowly
        6
    zlowly  
       2021-01-23 19:13:11 +08:00
    试试看 group by service_id having count(flag)=sum(strcmp(flag,'flag1'))
    zlowly
        7
    zlowly  
       2021-01-23 19:23:00 +08:00
    搞错了,STRCMP(expr1,expr2)相等时才 0,既然只有 flag1,flag2,所以应该直接时 having sum(strcmp(flag,'flag1'))=0
    liprais
        8
    liprais  
       2021-01-23 19:38:56 +08:00
    select
    *
    from
    (
    select
    a,
    max(case when b = 2 then b end) as flag1,
    max(case when b = 3 then b end) as flag2
    from flags
    group by a
    ) dt
    where
    flag1 is not null
    and
    flag2 is null

    行转列过滤一下就行
    iamxmz
        9
    iamxmz  
       2021-01-23 21:20:43 +08:00
    select service_id, count(flag), sum(flag) from service_info group by service_id having count(flag) = 1 and sum(flag) = flag1
    PopRain
        10
    PopRain  
       2021-01-23 22:07:47 +08:00
    select service_id, sum(case when flag='flag1' then 1 else 0 end) as f1, sum(case when flag='flag2' then 1 else 0 end) as f2
    from service_info group by service_id
    having sum(case when flag='flag1' then 1 else 0 end)=1 and sum(case when flag='flag2' then 1 else 0 end) =0
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2921 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 21ms · UTC 15:10 · PVG 23:10 · LAX 08:10 · JFK 11:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.