MySQL:在特定条件下更新表

MySQL:在特定条件下更新表,mysql,sql-update,sql-insert,Mysql,Sql Update,Sql Insert,我有两个表,主表和当前表(每小时刷新一次)。 两张桌子的结构相同: Chk |描述|状态|日期 我需要更新/追加(添加新行)到主表中,如果: 1) 具有新ID的行或 2) 如果某个特定变量(本例中为“状态”)已更改。我试着用下面的方法来做,但没有成功: INSERT into AGILE_TICKETS_DLY SELECT * FROM CURR_AGILE_TICKETS curr WHERE EXISTS (SELECT * FROM AGILE_TICKETS_DLY mstr

我有两个表,主表和当前表(每小时刷新一次)。 两张桌子的结构相同:

Chk |描述|状态|日期

我需要更新/追加(添加新行)到主表中,如果:

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))

有没有关于如何实现这一点的建议?

我尝试通过两个单独的步骤来实现这一点:

1) 首先,我用id追加所有新行:这很有效

INSERT  into AGILE_TICKETS_DLY 
 SELECT * FROM CURR_AGILE_TICKETS curr
  WHERE not EXISTS (SELECT * FROM AGILE_TICKETS_DLY mstr
          WHERE (curr.chk = mstr.chk));
但后来,我试着在下面做了一个错误

2) 然后用新值替换“State”变量:

 INSERT  into AGILE_TICKETS_DLY_1 (state)
SELECT state 
  from CURR_AGILE_TICKETS_1 curr
where exists ( select * from AGILE_TICKETS_DLY_1 mstr where curr.chk = 
       mstr.chk);
但这给了我一个错误:

SQL错误(1364):字段“chk”没有默认值


这是什么意思?

您可以尝试以下两种查询:

-- 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     |
+------+----------------+-----------------+----------------+

触发器可能是您需要的,谢谢,但即使在触发器中,我假设我必须正确地给出where条件,我相信这里的任何东西都无法检测到变化。。有人能说出WHERE条件下的错误吗?您使用的是
mstr.state=curr.state
而不是
mstr.state!=当前状态
可能是这样的:也会有带有新ID的行,因此如果我在重复键上使用,则我无法捕获该行。。