mysql 基础不牢,请教各位 v 友一个查询语句。 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
请不要在回答技术问题时复制粘贴 AI 生成的内容
fragrans23
V2EX    程序员

mysql 基础不牢,请教各位 v 友一个查询语句。

  •  
  •   fragrans23 2022-01-05 10:54:16 +08:00 3201 次点击
    这是一个创建于 1374 天前的主题,其中的信息可能已经有所发展或是发生改变。

    1.数据表有两个字段 bus_id 和 record_id ,record_id 唯一不重复,bus_id 可以对应多个 record_id 。现求 record_id 的数量,多个 bus_id 对应的 recordId 算一个数量。 2.不能直接求去重后 bus_id 的数量,其他业务可能没有 bus_id

    第 1 条附言    2022-01-05 14:29:38 +08:00
    | id | bus_id | record_id |
    | -- | ------ | --------- |
    | 1 | | xxx1 |
    | 2 | a | xxx2 |
    | 3 | b | xxx3 |
    | 4 | a | xxx4 |
    | 5 | c | xxx5 |
    | 6 | c | xxx6 |
    | 7 | | xxx7 |


    类似这种,查 record_id 数量,同一个 bus_id 对应多个的 record_id 计算时,只计算一个数量,比如 id 为 5 和 6 的 busId 相同,计算 record_id 数量时,record_id 为 1 ,不是 2 。
    17 条回复    2022-01-05 16:08:08 +08:00
    cwcc
        1
    cwcc  
       2022-01-05 10:59:09 +08:00
    1 我没太懂,record_id 都唯一不重复了,直接 count 应该就好?(或者你把唯一不重复的这个字段没设置成 primary ?那就不行)

    终极解决:3NF
    Xusually
        2
    Xusually  
       2022-01-05 11:01:04 +08:00
    最简单的 COUNT 满足不了么?还是我没理解需求?
    sunhelter
        3
    sunhelter  
       2022-01-05 11:01:18 +08:00
    不能直接去重就分别 union 咯

    SQL Server 可以这么写:
    select SUM(ct)
    select COUNT(1) ct from table where ISNULL(bus_id,'')!='' group by bus_id
    union all
    select COUNT(1) from table where ISNULL(bus_id,'')=''
    sunhelter
        4
    sunhelter  
       2022-01-05 11:02:11 +08:00
    上面的 SQL 在 SUM(ct)后加个括号,到结尾加反括号
    fragrans23
        5
    fragrans23  
    OP
       2022-01-05 12:00:38 +08:00
    @crazywhalecc 但是,如果一个 bus_id 对应多个 record_id,这种情况计数 record_id 只记一个,例如一个 bus_id 对应两个 record_id,count(record_id)时,只记一次。直接 count 会记录两次吧
    zheng96
        6
    zheng96  
       2022-01-05 12:23:57 +08:00
    前提是 bus_id 和 record_id 本身不会重复,如果会重复可以在 then 后头拼接个前缀区分下
    select count(
    distinct(
    case
    when record_id is null then null
    when bus_id is null then record_id
    else bus_id end
    )
    ) from table;
    fragrans23
        7
    fragrans23  
    OP
       2022-01-05 12:52:02 +08:00
    @Xusually 要考虑 bus_id 对应多个 record_id 的情况,对应多个的话,record_id 只记录一次
    fragrans23
        8
    fragrans23  
    OP
       2022-01-05 13:04:23 +08:00
    @zheng96 谢谢大佬,貌似可以,我得多试一下
    fragrans23
        9
    fragrans23  
    OP
       2022-01-05 13:09:21 +08:00
    @sunhelter 感谢回复,mysql 好像不行
    themostlazyman
        10
    themostlazyman  
       2022-01-05 13:34:49 +08:00
    问题描述没太懂,建议给一个最小化的表结构,再加几条典型数据来说明你的问题。
    xiangyuecn
        11
    xiangyuecn  
       2022-01-05 13:43:40 +08:00
    “不能直接求去重后 bus_id 的数量,其他业务可能没有 bus_id”

    分两条简单的 sql 语句搞定,没必要写一条蹩脚的 sql
    JKeita
        12
    JKeita  
       2022-01-05 13:59:50 +08:00
    同没看懂题目
    newtype0092
        13
    newtype0092  
       2022-01-05 14:10:45 +08:00
    (select count(*) from table where bus_id is null) + (select count(*) from table where bus_is is not null group by bus_id)
    zhuangjia
        14
    zhuangjia  
       2022-01-05 14:11:55 +08:00
    题目中的描述部分 “多个 bus_id 对应的 recordId 算一个数量” 有歧义,楼主在 #7 给了补充描述:“bus_id 对应的 多个 recordId 算一个数量”。
    ray5173
        15
    ray5173  
       2022-01-05 14:15:04 +08:00
    select count(distinct a.record_id)
    from a
    left join b on a.record_id = b.bus_id
    where b.bus_id is not null
    zhuangjia
        16
    zhuangjia  
       2022-01-05 14:15:15 +08:00   2
    @fragrans23

    //最小化表结构:
    CREATE TABLE `bus_test` (
    `record_id` int(11) NOT NULL AUTO_INCREMENT,
    `bus_id` int(11) DEFAULT '0',
    PRIMARY KEY (`record_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    //测试数据
    INSERT INTO `test`.`bus_test` (`bus_id`) VALUES (FLOOR(RAND() * 10));

    //查询 SQL
    select sum(case when t.bus_id is null then t.record_count else 1 end) from (SELECT count(record_id) as record_count,bus_id FROM `bus_test` group by bus_id) t;
    fragrans23
        17
    fragrans23  
    OP
       2022-01-05 16:08:08 +08:00
    @zhuangjia 感谢回复
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     1210 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 17:27 PVG 01:27 LAX 10:27 JFK 13:27
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86