Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 根据计数查询的结果插入记录数_Mysql_Sql - Fatal编程技术网

Mysql 根据计数查询的结果插入记录数

Mysql 根据计数查询的结果插入记录数,mysql,sql,Mysql,Sql,我有一个应用程序跟踪管理员在和admin\u comments表中采取的操作 最近,我被要求创建一个新的表admin_stats,用于跟踪应用程序某些区域的生产率 我已经在我的代码逻辑中实现了这一点,因此所有统计数据都会被跟踪,但他们问我是否可以通过搜索admin\u comments表中的适当字段来添加所有以前的统计数据 我的admin\u comments表如下所示: +----+---------+----------+---------+------+ | id | user_id |

我有一个应用程序跟踪管理员在和
admin\u comments
表中采取的操作

最近,我被要求创建一个新的表
admin_stats
,用于跟踪应用程序某些区域的生产率

我已经在我的代码逻辑中实现了这一点,因此所有统计数据都会被跟踪,但他们问我是否可以通过搜索
admin\u comments
表中的适当字段来添加所有以前的统计数据

我的
admin\u comments
表如下所示:

+----+---------+----------+---------+------+
| id | user_id | admin_id | comment | type |
+----+---------+----------+---------+------+
+----+----------+---------+------+--------+
| id | admin_id | user_id | type | action |
+----+----------+---------+------+--------+
admin\u stats
表如下所示:

+----+---------+----------+---------+------+
| id | user_id | admin_id | comment | type |
+----+---------+----------+---------+------+
+----+----------+---------+------+--------+
| id | admin_id | user_id | type | action |
+----+----------+---------+------+--------+
首先,我只想将索赔导入到此表中。索赔被
批准
拒绝
临时拒绝
,或
永久拒绝
,我已使用此查询从管理员处获得了每个操作的计数:

select admin_id, 
    SUM(CASE WHEN comment like 'Approved the claim for%' THEN 1 ELSE 0 END) as Approved,
    SUM(CASE WHEN comment like 'Rejected the claim for%' THEN 1 ELSE 0 END) as Rejected,
    SUM(CASE WHEN comment like 'Temporarily rejected (24 hour hold) the claim for%' THEN 1 ELSE 0 END) as TempReject, 
    SUM(CASE WHEN comment like 'Permanently rejected the claim for%' THEN 1 ELSE 0 END ) as PermReject
from admin_comments
where admin_id > 0
group by admin_id
产生如下结果:

+----+---------+----------+---------+------+
| id | user_id | admin_id | comment | type |
+----+---------+----------+---------+------+
+----+----------+---------+------+--------+
| id | admin_id | user_id | type | action |
+----+----------+---------+------+--------+

是否有办法根据每个
管理员id的计数执行插入

i、 e从上图中,我想在
admin\u stats
中插入一条记录,用于
类型为
的已批准
操作
作为应用程序,一条记录用于
管理id为
的已拒绝
操作
应用程序

对于
admin\u id
4,我希望插入25个已批准的
操作
,12个已拒绝的
操作
,以及2个
临时拒绝
操作


因此,每个操作都应作为新行输入,
类型将始终是应用程序。

我认为您正在尝试这样做

insert into admin_stats(admin_id,type,action) --add other columns as needed
select admin_id,'application' as type,
case when comment like 'Approved the claim for%' then 'Approved'
     when comment like 'Rejected the claim for%' then 'Rejected'
     when comment like 'Temporarily rejected (24 hour hold) the claim for%' then 'TempReject'
     when comment like 'Permanently rejected the claim for%' then 'PermReject'
end as action
from admin_comments
where admin_id > 0
and (comment like 'Approved the claim for%'
     or comment like 'Rejected the claim for%'
     or comment like 'Temporarily rejected (24 hour hold) the claim for%'
     or comment like 'Permanently rejected the claim for%')