作者cutekid (可爱小孩子)
看板Database
标题Re: [SQL ] substring语法
时间Tue Jul 15 14:12:49 2014
select t4.xxx,
ifnull(t5.cnt,0)
from
(
select @xxx := @xxx + 1 as xxx
from (
select 1 union select 2 union select 3 union
select 4 union select 5 union select 6 union
select 7 union select 8 union select 9 union
select 10
) t1,(
select 1 union select 2 union select 3 union
select 4 union select 5 union select 6 union
select 7 union select 8 union select 9
) t2,(
select @xxx:=9
) t3
) t4 left join (
select rpad(栏位名称,2,'0') as xxx,count(*) as cnt
from 表格名称
group by rpad(栏位名称,2,'0')
) t5 on t4.xxx = t5.xxx
注: 黄色部份是产生 10 ~ 99
粉红部份是缺的部份补 0
※ 引述《KerKerLaugh (科科笑)》之铭言:
: 另外我想再接着问一个问题
: 我用rpad取该栏位的前两位数时应该会有10~99共90笔资料 计算COUNT
: 如果原始资料表里缺少了51~55开头的这5笔资料
: 那麽XXX只会显示 ~ 49 50 56 57 ~
: 是否有办法补上缺的位数 然後该笔COUNT=0
: ※ 引述《cutekid (可爱小孩子)》之铭言:
: : select rpad(栏位名称,2,'0') as xxx,count(*)
: : from 表格名称
: : group by rpad(栏位名称,2,'0')
: : ref. bohei
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 210.61.233.210
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Database/M.1405404772.A.1E6.html
1F:推 KerKerLaugh:感谢你 07/16 09:21