具有链接表的MySQL更新更新联接列

时间:2018-08-12 09:29:12

标签: mysql sql-update update-statement

我有一个具有以下结构的表tbl_sc_3_4_ps(id_team的多个条目可能在一个c_minute内输入!!!):

+--------+---------+--------+----------+------+---------------------+
| id     | id_team | bill_n | bill_min | ps   | c_minute            |
+--------+---------+--------+----------+------+---------------------+
| 175019 | team1   | 0.0489 |     NULL | NULL | 2018-08-07 00:00:00 |
| 175020 | team1   | 0.0500 |     NULL | NULL | 2018-08-07 00:01:00 |
| 175021 | team1   | 0.0464 |     NULL | NULL | 2018-08-07 00:02:00 |
| 175022 | team1   | 0.0392 |     NULL | NULL | 2018-08-07 00:03:00 |
| 175023 | team1   | 0.0504 |     NULL | NULL | 2018-08-07 00:04:00 |
| 175024 | team1   | 0.0484 |     NULL | NULL | 2018-08-07 00:05:00 |
| 175025 | team1   | 0.0475 |     NULL | NULL | 2018-08-07 00:06:00 |
| 175026 | team1   | 0.0500 |     NULL | NULL | 2018-08-07 00:07:00 |
| 175027 | team1   | 0.0453 |     NULL | NULL | 2018-08-07 00:08:00 |
| 175028 | team1   | 0.0445 |     NULL | NULL | 2018-08-07 00:09:00 |
+--------+---------+--------+----------+------+---------------------+

,并希望从tbl_sc_3_4_bill_min中更新bill_min列(一c_minute只能有一个bill_min,c_minute是pkey):

+---------------------+----------+
| c_minute            | bill_min |
+---------------------+----------+
| 2018-08-07 00:00:00 |   0.0418 |
| 2018-08-07 00:01:00 |   0.0766 |
| 2018-08-07 00:02:00 |   0.1188 |
| 2018-08-07 00:03:00 |   0.1556 |
| 2018-08-07 00:04:00 |   0.2002 |
| 2018-08-07 00:05:00 |   0.2416 |
| 2018-08-07 00:06:00 |   0.2779 |
| 2018-08-07 00:07:00 |   0.3187 |
| 2018-08-07 00:08:00 |   0.3614 |
| 2018-08-07 00:09:00 |   0.4000 |
+---------------------+----------+

我的SQL语句:

update dbsdme.tbl_su_3_4_ps as a
inner join dbsdme.tbl_sc_3_4_bill_min as b on b.c_minute = a.c_minute
set a.bill_min = b.bill_min
where a.bill_min is NULL;

这不仅会更新bill_min列,还会更新c_minute列(带有执行查询时的时间戳):

+--------+---------+--------+----------+------+---------------------+
| id     | id_team | bill_n | bill_min | ps   | c_minute            |
+--------+---------+--------+----------+------+---------------------+
| 175019 | team1   | 0.0489 |   0.0418 | NULL | 2018-08-12 10:29:31 |
| 175020 | team1   | 0.0500 |   0.0766 | NULL | 2018-08-12 10:29:31 |
| 175021 | team1   | 0.0464 |   0.1188 | NULL | 2018-08-12 10:29:31 |
| 175022 | team1   | 0.0392 |   0.1556 | NULL | 2018-08-12 10:29:31 |
| 175023 | team1   | 0.0504 |   0.2002 | NULL | 2018-08-12 10:29:31 |
| 175024 | team1   | 0.0484 |   0.2416 | NULL | 2018-08-12 10:29:31 |
| 175025 | team1   | 0.0475 |   0.2779 | NULL | 2018-08-12 10:29:31 |
| 175026 | team1   | 0.0500 |   0.3187 | NULL | 2018-08-12 10:29:31 |
| 175027 | team1   | 0.0453 |   0.3614 | NULL | 2018-08-12 10:29:31 |
| 175028 | team1   | 0.0445 |   0.4000 | NULL | 2018-08-12 10:29:31 |
+--------+---------+--------+----------+------+---------------------+

有人知道为什么还要更新c_minute列吗?我只想更新bill_min。

最诚挚的问候

Jan

0 个答案:

没有答案