mysql - sql 统计某列下的4个值,分别是多少个
黄舟
黄舟 2017-04-17 15:29:10
[MySQL讨论组]

dg_order_time下的4个值 1,2,3,4他们各自的总和是多少个?是各自,不是这列的总和...

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
ringa_lee

你是要实现每一行数据中的所有相同数的总和吗?所有行的1的总和;所有行2的总和...。是不!

天蓬老师

这是横的

select a.t1, b.t2, c.t3, d.t4
from (select count(*) t1 from table where col like '%1%') a,
(select count(*) t2 from table where col like '%2%') b,
(select count(*) t3 from table where col like '%3%') c,
(select count(*) t4 from table where col like '%4%') d

这是竖的

select 't1', count(*) from table where col like '%1%'
union all
select 't2', count(*) from table where col like '%2%'
union all
select 't3', count(*) from table where col like '%3%'
union all
select 't4', count(*) from table where col like '%4%'

大致思路就是这样

大家讲道理
select nvl(substr(t.str, 0, 1),0) +
       nvl(substr(t.str, (case when instr(t.str, ',', 1, 1) < 0 then -1
                 when instr(t.str, ',', 1, 1) > 0 then instr(t.str, ',', 1, 1)+1 end), 1),0) +
       nvl(substr(t.str, (case when instr(t.str, ',', 1, 2) < 0 then -1
                 when instr(t.str, ',', 1, 2) > 0 then instr(t.str, ',', 1, 2)+1 end), 1),0) +
       nvl(substr(t.str, (case when instr(t.str, ',', 1, 3) < 0 then -1
                 when instr(t.str, ',', 1, 3) > 0 then instr(t.str, ',', 1, 3)+1 end), 1),0)
  from test_calc t;

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号