如何在mysql中对多行执行操作

时间:2018-08-21 05:36:04

标签: mysql

我有一个轮班时间表表,用于员工每月明智的轮班。在此表中,共有34个字段。

表头是:

EmpNo |Month|year|Day1|Day2|Day3|Day4|Day5|Day6|Day7|Day8|Day9|Day10|Day11|Day12|Day13|Day14|Day15|Day16|Day17|Day18|Day19|Day20|Day21|Day22|Day23|Day24|Day25|Day26|Day27|Day28|Day29|Day30|Day31

表格数据:

101|06|2018|A1|A1|A1|H|A1|WO|WO|A1|A1|A1|A1|A1|WO|WO|A1|A1|A1|H|A1|WO|WO|A1|A1|A1|A1|A1|WO|WO|A1|A1
102|06|2018|A1|A1|H|A1|A1|WO|WO|A1|A1|A1|A1|A1|WO|WO|A1|A1|A1|H|A1|WO|WO|A1|A1|A1|A1|A1|WO|WO|A1|A1
103|06|2018|A1|A1|A1|A1|A1|WO|WO|A1|A1|A1|A1|A1|WO|WO|A1|A1|A1|H|A1|WO|WO|A1|A1|A1|A1|A1|WO|WO|A1|A1

HERE A1 是班次代码, WO 是休假, H 是假期。

我需要明智地计算一周休假和每个月员工休假的总数。

1 个答案:

答案 0 :(得分:0)

您可以执行类似How to count items in comma separated list MySQL的操作,其中使用length和replace来计算出现的次数。在您的情况下,它稍微复杂一些(但不多),您必须顺应这一天并满足空值-我建议每天添加一个ifnull测试并替换一个虚拟值“ x”。

drop table if exists t;
create table t(empid varchar(3),month varchar(2), year varchar(4),
day1 varchar(2), day2 varchar(2) ,day3 varchar(2),day4 varchar(2), day5 varchar(2), day6 varchar(2),
day7 varchar(2), day8 varchar(2) ,day9 varchar(2),day10 varchar(2), day11 varchar(2), day12 varchar(2),
day13 varchar(2), day14 varchar(2), day15 varchar(2),day16 varchar(2), day17 varchar(2), day18 varchar(2),
day19 varchar(2), day20 varchar(2), day21 varchar(2),day22 varchar(2), day23 varchar(2), day24 varchar(2),
day25 varchar(2), day26 varchar(2), day27 varchar(2),day28 varchar(2), day29 varchar(2), day30 varchar(2));
#day31 varchar(2));
insert into t values
('101','06','2018','A1','A1','A1','H','A1','WO','WO','A1','A1','A1','A1','A1','WO','WO','A1','A1','A1','H','A1','WO','WO','A1','A1','A1','A1','A1','WO','WO','A1','A1'),
('102','06','2018','A1','A1','H','A1','A1','WO','WO','A1','A1','A1','A1','A1','WO','WO','A1','A1','A1','H','A1','WO','WO','A1','A1','A1','A1','A1','WO','WO','A1','A1'),
('103','06','2018','A1','A1','A1','A1','A1','WO','WO','A1','A1','A1','A1','A1','WO','WO','A1','A1','A1','H','A1','WO','WO','A1','A1','A1','A1','A1','WO','WO','A1','A1');

select empid,month,year,
         length(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30)) len,
         length(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30)) -
            length(replace(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30),
                            'H','')) h,
                ( length(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30)) -
            length(replace(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30),
                            'WO',''))
                    ) /2  wo,

                 length(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30)) -
            length(replace(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30),
                            'H','')) +
                ( length(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30)) -
            length(replace(concat(day1,day2,day3,day4,day5,day6,day7,day8,day9,day10,
                            day11,day12,day13,day14,day15,day16,day17,day18,day19,day20,
                            day21,day22,day23,day24,day25,day26,day27,day28,day29,day30),
                            'WO',''))
                    ) /2   off
from t;

+-------+-------+------+------+------+--------+---------+
| empid | month | year | len  | h    | wo     | off     |
+-------+-------+------+------+------+--------+---------+
| 101   | 06    | 2018 |   58 |    2 | 8.0000 | 10.0000 |
| 102   | 06    | 2018 |   58 |    2 | 8.0000 | 10.0000 |
| 103   | 06    | 2018 |   59 |    1 | 8.0000 |  9.0000 |
+-------+-------+------+------+------+--------+---------+
3 rows in set (0.00 sec)