MySQL : Update Table upon certain conditions

标签 mysql sql-update sql-insert

我有 2 个表,主表和当前表(非常小时刷新)。 两个表都具有相同的结构:

检查 |描述 |状态 |日期

如果出现以下情况,我需要更新/附加(添加新行)到主表中:

1) 具有新 ID 的行或

2) 如果特定变量(在本例中为“状态”)已更改。我试着用下面的方法来做,但没有成功:

INSERT  into AGILE_TICKETS_DLY 
SELECT * FROM CURR_AGILE_TICKETS curr
  WHERE EXISTS (SELECT * FROM AGILE_TICKETS_DLY mstr
          WHERE (curr.chk != mstr.chk) OR ( curr.chk = mstr.chk and 
    mstr.state != curr.state))

关于如何实现这一目标的任何指示?

最佳答案

你可以试试这对查询:

-- insert new rows
insert into agile_tickets_dly
select * from curr_agile_tickets 
where chk not in (select chk from agile_tickets_dly);

-- update updated rows
update agile_tickets_dly x
join 
(
    select b.chk chk,b.description description,b.state state,b.date date 
    from agile_tickets_dly a, curr_agile_tickets b
    where 
        a.chk=b.chk and
        (a.description != b.description or a.state != b.state or a.date != b.date)
) y
on x.chk=y.chk
set x.description = y.description, x.state= y.state, x.date = y.date;

插图:

select * from agile_tickets_dly;
+------+-------------+---------+------------+
| chk  | description | state   | date       |
+------+-------------+---------+------------+
| 0    | desc-0      | state-1 | 01-01-2017 |
| 1    | desc-1      | state-1 | 01-01-2018 |
| 2    | desc-2      | state-2 | 01-02-2018 |
| 3    | desc-3      | state-3 | 01-03-2018 |
+------+-------------+---------+------------+

-- one new row with chk=4, three updated rows with chk=1,2,3
select * from curr_agile_tickets;
+------+----------------+-----------------+----------------+
| chk  | description    | state           | date           |
+------+----------------+-----------------+----------------+
| 0    | desc-0         | state-1         | 01-01-2017     |
| 1    | desc-1         | state-1         | date-1-updated |
| 2    | desc-2-updated | state-2         | 01-02-2018     |
| 3    | desc-3         | state-3-updated | 01-03-2018     |
| 4    | desc-4         | state-4         | 01-04-2018     |
+------+----------------+-----------------+----------------+

-- after executing the two queries
select * from agile_tickets_dly;
+------+----------------+-----------------+----------------+
| chk  | description    | state           | date           |
+------+----------------+-----------------+----------------+
| 0    | desc-0         | state-1         | 01-01-2017     |
| 1    | desc-1         | state-1         | date-1-updated |
| 2    | desc-2-updated | state-2         | 01-02-2018     |
| 3    | desc-3         | state-3-updated | 01-03-2018     |
| 4    | desc-4         | state-4         | 01-04-2018     |
+------+----------------+-----------------+----------------+

关于MySQL : Update Table upon certain conditions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50140556/

相关文章:

sql-update - 使用选择查询更新表

php - mySQL用相同的值更新多行

function - 如何在 PostgreSQL 中使用函数插入多行

sql - MySQL:获取父子结构的根节点

PHP 图像上传可以在本地主机上运行,​​但不能在服务器上运行

mysql - 更新表 mysql,如果值为 10,则更新另一个表

mysql - MySQL中如何用原始数据查找被截断的数据

python - 如何加快MySQL更新速度

C# & SQL Server : how to Insert DBNull. 如果字符串值为空,命令参数中的值?

sql-server - 触发添加修改日期的插入和更新