我正在尝试使用MySQL视图提取报告,但每个用户都会重复一个新行,如下图所示
你能帮我改写一下这个问题来解决这个问题吗?
DROP FUNCTION IF EXISTS adminExportFromDate;
DROP FUNCTION IF EXISTS adminExportToDate;
create function adminExportFromDate() returns DATETIME DETERMINISTIC NO SQL return @adminExportFromDate;
create function adminExportToDate() returns DATETIME DETERMINISTIC NO SQL return @adminExportToDate;
create or replace view admin_export as
Select admin.id as id ,CONCAT(admin.first_name," ",admin.last_name) as Name ,
count(DISTINCT `all_jobs`.`id`)AS all_jobs_count,
sum( case when `all_jobs`.job_status_id = 25 then 1 else 0 end ) as canceled_jobs_count,
sum( case when `all_jobs`.job_status_id in (31,30,3) then 1 else 0 end ) as finished_jobs_count,
0 as deleted_jobs_count
From admin
INNER JOIN job all_jobs on all_jobs.created_by_id = admin.id and all_jobs.created_by_level = \'admin\'
and `all_jobs`.`request_date` > AdminExportFromDate() AND `all_jobs`.`request_date` < AdminExportToDate()
group by admin.id
UNION ALL
select admin.id,CONCAT(admin.first_name," ",admin.last_name) as Name,
0 AS all_jobs_count,
0 as canceled_jobs_count,
0 as finished_jobs_count,
count( deleted_log.id ) as deleted_jobs_count
From admin INNER JOIN deleted_log
on deleted_log.`type` LIKE \'job\' and data RLIKE CONCAT(\'"created_by_level":"admin","created_by_id":"\',`admin`.`id`,\'"\') and `deleted_log`.`created_at` > AdminExportFromDate() AND `deleted_log`.`created_at` < AdminExportToDate()
group by admin.id
这是观点的结果:
答案 0 :(得分:0)
UNION ALL
/ UNION
会创建多行。您想要将行组合在一起。这通常涉及JOIN
。
在这种情况下,我认为这样的事情符合你的要求:
Select a.id as id, CONCAT(a.first_name, ' ', a.last_name) as Name,
coalesce(j.all_jobs_count, 0) as all_jobs_count,
coalesce(j.canceled_jobs_count, 0) as canceled_jobs_count,
coalesce(j.finished_jobs_count, 0) as finished_jobs_count,
coalesce(0, deleted_jobs_count) as deleted_jobs_count
From admin a left join
(select j.created_by_id,
count(distinct j.id) as all_jobs_count,
sum( j.job_status_id = 25 ) as canceled_jobs_count,
sum( j.job_status_id in (31, 30, 3) ) as finished_jobs_count
from job j
where j.created_by_level = \'admin\' and
j.request_date > AdminExportFromDate() and
j.request_date < AdminExportToDate()
group by j.created_by_id
) j
on j.created_by_id = a.id left join
(select dl.data, count(*) as deleted_jobs_count
from deleted_log dl
where dl.`type` LIKE \'job\' and
dl.`created_at` > AdminExportFromDate() AND
dl.`created_at` < AdminExportToDate()
group by dl.data
) dl
on dl.data RLIKE CONCAT(\'"created_by_level":"admin", "created_by_id":"\', a.id, \'"\')
编辑:
上面显然没有为MySQL中的视图工作(arggh!)。可以使用from
子句中的子查询代替:
Select a.id as id, CONCAT(a.first_name, ' ', a.last_name) as Name,
(select count(*)
from jobs j
where j.created_by_id = a.id and
j.created_by_level = \'admin\' and
j.request_date > AdminExportFromDate() and
j.request_date < AdminExportToDate()
) as all_jobs_count,
(select count(*)
from jobs j
where j.created_by_id = a.id and
j.created_by_level = \'admin\' and
j.request_date > AdminExportFromDate() and
j.request_date < AdminExportToDate() and
j.job_status = 25
) as canceled_jobs_count,
(select count(*)
from jobs j
where j.created_by_id = a.id and
j.created_by_level = \'admin\' and
j.request_date > AdminExportFromDate() and
j.request_date < AdminExportToDate() and
j.job_status in (31, 30, 3)
) as finished_jobs_count,
(select count(*) as deleted_jobs_count
from deleted_log dl
where dl.data rlike concat(\'"created_by_level":"admin", "created_by_id":"\', a.id, \'"\') and
dl.`type` like \'job\' and
dl.`created_at` > AdminExportFromDate() AND
dl.`created_at` < AdminExportToDate()
) as deleted_jobs_count
From admin a