on duplicate key update

简介: INSERT INTO issue_change(project_id, add_date, change_amount, base_line, gmt_create , gmt_modified)SELECT p.

INSERT INTO issue_change(project_id, add_date, change_amount, base_line, gmt_create , gmt_modified)
SELECT p.id project_id,
date(i.commit_date) add_date,
@changeAmount := count() change_amount,
@baseLine := (SELECT
round(
(
SELECT
ifnull(count(
),@changeAmount)
FROM
issue i,
project p
WHERE
p.deparmtent_id = i.dept_no
AND p.id = project_id
AND YEAR (i.commit_date) = YEAR (add_date)
AND MONTH (i.commit_date) = MONTH (add_date) - 1
and i.is_deleted ='n'
and i.is_valid ='Y'
and i.template_id != 100373
) / (
SELECT
sum(asccr.line_count)
FROM
aone_statistics_code_commit_record asccr,
change_request cr,
project p
WHERE
asccr.branch_url = cr.branch_url
AND p.deparmtent_id = cr.dept_no
AND p.id = 1
AND YEAR (asccr.commit_time) = YEAR (add_date)
AND MONTH (asccr.commit_time) = MONTH (add_date) - 1
and cr.is_deleted ='n'
) * (
SELECT
sum(asccr.line_count)
FROM
aone_statistics_code_commit_record asccr,
change_request cr,
project p
WHERE
cr.branch_url = asccr.branch_url
AND p.aone_project_id = cr.aone_project_id
AND p.id = project_id
and cr.is_deleted ='n'
)
)) base_line,
now() gmt_create,
now() gmt_modified
FROM issue i
JOIN project p on p.aone_project_id = i.aone_project_id
where i.is_valid ='Y'
and i.template_id != 100373
and p.id in (SELECT id from project WHERE type > 499)
GROUP BY p.id, date(i.commit_date)
on duplicate key update change_amount = @changeAmount, base_line = @baseLine, gmt_modified = now()

其中,project_id,add_date 两个字段联合键为唯一约束。
alter table issue_change
add unique key uk_project_id_add_date (project_id,add_date);

声明SQL变量:
INSERT INTO issue_change(project_id, add_date, change_amount, base_line, gmt_create , gmt_modified)
SELECT p.id project_id,
date(i.commit_date) add_date,
@changeAmount := count(*) change_amount,
@baseLine := 1
...

on duplicate key update change_amount = @changeAmount, base_line = @baseLine, gmt_modified = now()

统计SQL sum,count

select p.name, round(sum(case when i.is_regression_stage_issue='Y' then 1 else 0 end)/count(i.id) *100,2) c from issue i

join project p on p.aone_project_id = i.aone_project_id

where p.deparmtent_id = 20862

and p.status = 1

and i.is_valid = 'Y'

and i.template_id != 100373

group by name order by c desc limit 3;

相关文章
|
关系型数据库 MySQL
Mysql 主键冲突(ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY')
Mysql 主键冲突(ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY')
1141 0
|
6月前
|
数据库 Python
Duplicate entry for key username
Duplicate entry for key username
70 0
|
11月前
|
数据库
MongoError: E11000 duplicate key error collection: blog.users index: email_1 dup key
MongoError: E11000 duplicate key error collection: blog.users index: email_1 dup key
|
11月前
|
SQL 关系型数据库 MySQL
INSERT ... ON DUPLICATE KEY UPDATE Statement
INSERT ... ON DUPLICATE KEY UPDATE Statement
78 0
|
关系型数据库 MySQL 数据库
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
解决出现的SQLIntegrityConstraintViolationExceptionw:Duplicate entry ‘10‘ for for key ‘user.PRIMARY‘问题
204 0
|
数据可视化 关系型数据库 MySQL
MySql命令报错:Duplicate entry ‘10‘ for key ‘PRIMARY‘解决方案
MySql命令报错:Duplicate entry ‘10‘ for key ‘PRIMARY‘解决方案
545 0
|
SQL 数据库
ON DUPLICATE KEY UPDATE
ON DUPLICATE KEY UPDATE
134 0
ON DUPLICATE KEY UPDATE
|
存储 关系型数据库 MySQL
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
3662 0
超详细!Mysql错误1452 - Cannot add or update a child row: a foreign key constraint fails 原因及解决方法
|
关系型数据库 MySQL
MySQL - Duplicate entry for key PRIMARY
MySQL - Duplicate entry for key PRIMARY
316 0
|
关系型数据库 MySQL 数据库
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
MySQL问题解决:Cannot delete or update a parent row: a foreign key constraint fails
1294 0