高级 SQL

版本 v1

select
  sum(if(type = 1, 1, 0)) 入职,
  sum(if(type = 2, 1, 0)) 转正,
  sum(if(type = 10, 1, 0)) 离职,
  sum(if(type = 12, 1, 0)) 调岗,
  sum(if(type = 15, 1, 0)) 复职,
  operator_id
from growth_track
group by operator_id;

版本 V2

select
  sum(case type when 1 then 1 else 0 end) aa,
  sum(case type when 15 then 1 else 0 end) ee,
  group_concat(case type when 1 then staff_id else null end) a,
  group_concat(case type when 15 then staff_id else null end) e,
  operator_id
from growth_track
group by operator_id, date(create_time)
order by date(create_time) desc
limit 12;

版本 V3

select
  sum(case gt.type when 1 then 1 else 0 end) ruzhi,
  group_concat(case gt.type when 1 then st.real_name else null end) ruzhiren,
  sum(case gt.type when 10 then 1 else 0 end) lizhi,
  group_concat(case gt.type when 10 then st.real_name else null end) lizhiren,
  gt.operator_id,
    gt.create_time
from growth_track as gt, staff as st
where gt.staff_id = st.id
group by gt.operator_id,date(gt.create_time)
order by date(gt.create_time) desc

版本 V4

SELECT
    sum( CASE gt.type WHEN 1 THEN 1 ELSE 0 END ) ruzhi,
    sum( CASE gt.type WHEN 10 THEN 1 ELSE 0 END ) lizhi,
    sum( CASE gt.type WHEN 15 THEN 1 ELSE 0 END ) fuzhi,
    sum( CASE gt.type WHEN 12 THEN 1 ELSE 0 END ) tiaogang,
    sum( CASE gt.type WHEN 2 THEN 1 ELSE 0 END ) zhuanzheng,
    group_concat( CASE gt.type WHEN 1 THEN st.real_name ELSE NULL END ) ruzhiren,
    group_concat( CASE gt.type WHEN 10 THEN st.real_name ELSE NULL END ) lizhiren,
    oper.real_name as caozuoren,
    date( gt.create_time ) 
FROM
    growth_track as gt left join staff as st 
on
  gt.staff_id = st.id left join staff as oper
    on gt.operator_id = oper.id
GROUP BY
    gt.operator_id,
    date( gt.create_time ) 
ORDER BY
    date( gt.create_time ) DESC 

版本 V5

SELECT
    sum( CASE gt.type WHEN 1 THEN 1 ELSE 0 END ) ruzhi,
    sum( CASE gt.type WHEN 10 THEN 1 ELSE 0 END ) lizhi,
    sum( CASE gt.type WHEN 15 THEN 1 ELSE 0 END ) fuzhi,
    sum( CASE gt.type WHEN 12 THEN 1 ELSE 0 END ) tiaogang,
    sum( CASE gt.type WHEN 2 THEN 1 ELSE 0 END ) zhuanzheng,
    group_concat( CASE gt.type WHEN 1 THEN st.real_name ELSE NULL END ) ruzhiren,
    group_concat( CASE gt.type WHEN 10 THEN st.real_name ELSE NULL END ) lizhiren,
    oper.real_name as caozuoren,
    date( gt.create_time ) 
FROM
    growth_track as gt left join staff as st 
on
  gt.staff_id = st.id  left join staff as oper
    on gt.operator_id = oper.id
WHERE
    ( gt.create_time BETWEEN '2018-07-30 00:00:00' AND '2018-08-06 23:59:59' ) 
    AND gt.delete_time IS NULL 
GROUP BY
    gt.operator_id,
    date( gt.create_time ) 

ORDER BY
    date( gt.create_time ) DESC 
update salary_commission sc
  inner join staff s on s.id = sc.staff_id
set sc.staff_name = concat(s.real_name, '11')
where sc.id between 1 and 10
update growth_track gt inner join staff s
    on s.real_name = substring_index(substring_index(substring_index(content, '由', -1), '为', 1), '(', 1)
    set beicaozuoid = s.id

添加字段

ALTER TABLE `growth_track` 
ADD COLUMN `department_id` int(0) UNSIGNED NOT NULL COMMENT '部门id' AFTER `staff_id`,
ADD INDEX `department_id`(`department_id`) USING BTREE;

猜你喜欢

转载自blog.csdn.net/Json159/article/details/81449402