简体   繁体   English

当对SQL UPDATE语句使用CASE时,为什么将NULL值输入数据库?

[英]Why is the value NULL entered into my database when I use CASE for my SQL UPDATE statement?

To prevent multiple SQL statements from being sent to the database like this: 为防止将多个SQL语句发送到数据库,如下所示:

UPDATE `gmw-db-051514`.`gmw_provider`
SET `gmw_provider`.`is_partner` = TRUE,
`partner_url` = 'some-url',
`partner_remove_token` = 'ABC123'
WHERE `gmw_provider`.`provider_id` = '11';

UPDATE `gmw-db-051514`.`tbl_conversations`
SET `tbl_conversations`.`partner_initial_id` = 30
WHERE `tbl_conversations`.`user_initial_id` = 30;

UPDATE `gmw-db-051514`.`tbl_conversations`
SET `tbl_conversations`.`partner_target_id` = 30
WHERE `tbl_conversations`.`user_following_id` = 30;

... I would like to merge these SQL statements into one. ...我想将这些SQL语句合并为一个。 I know that I can use IF and CASE in MYSQL statements but I think I'm using it wrong. 我知道我可以在MYSQL语句中使用IF和CASE,但我认为使用错了。

This is my attempt to use the CASE statement 这是我尝试使用CASE语句

UPDATE
  `gmw-db-051514`.`gmw_provider`,
  `gmw-db-051514`.`tbl_conversations`
SET
  `gmw_provider`.`is_partner` = true,
  `partner_url` = 'some-url',
  `partner_remove_token` = 'ABC123',
  `tbl_conversations`.`partner_initial_id` =
CASE WHEN 
  `tbl_conversations`.`user_initial_id` = '30' THEN '30' END,
  `tbl_conversations`.`partner_target_id` =
CASE WHEN 
  `tbl_conversations`.`user_following_id` = '30' THEN '30' END
WHERE
  `gmw_provider`.`provider_id` = '11'

The Condition works, but if there is already a value in a row, it is rewritten to null. 条件有效,但是如果行中已经存在一个值,则将其重写为null。

Have a look at this table: 看看这张桌子:

 |user_initial_id|user_following_id|partner_initial_id|partner_target_id|
 |     30        |        41       |       NULL       |        41       |
 |     51        |        41       |       NULL       |        41       |
 |     37        |        30       |       NULL       |        NULL     |

After executing the UPDATE Statement it looks like this (41 is gone) 执行UPDATE语句后,看起来像这样(41消失了)

 |user_initial_id|user_following_id|partner_initial_id|partner_target_id|
 |     30        |        41       |       30         |        NULL     |
 |     51        |        41       |       NULL       |        NULL     |
 |     37        |        30       |       NULL       |        30       |

But it should look like this (41 still there and 30 added): 但它看起来应该像这样(41个仍然存在,并添加了30个):

 |user_initial_id|user_following_id|partner_initial_id|partner_target_id|
 |     30        |        41       |       30         |        41       |
 |     51        |        41       |       NULL       |        41       |
 |     37        |        30       |       NULL       |        30       |

The row user_initial_id corresponds to partner_initial_id . user_initial_id行对应于partner_initial_id

The row user_following_id corresponds to partner_target_id . user_following_id行对应于partner_target_id

Like this: 像这样:

        A                 B                 A                  B
 |user_initial_id|user_following_id|partner_initial_id|partner_target_id|

Can you help me out with this or another attempt? 您能帮我这个还是其他尝试? The "IF" statement leads to the same result. “ IF”语句导致相同的结果。

Of course I used google and found similar questions here on stackoverflow, but the examples were more simple and not in combination with another table. 当然,我使用了google,并在stackoverflow上找到了类似的问题,但示例更加简单,没有与其他表结合使用。 I was afraid that this could affect the exact solution for these conditions. 我担心这会影响这些情况的确切解决方案。

If you can't help me, I use the multi-query version but this looks very ugly to me. 如果您不能帮我,我会使用多查询版本,但这对我来说看起来很丑。

Thanks! 谢谢!

EDIT: My problem was solved and I've learned something new! 编辑:我的问题解决了,我学到了新的东西! If someone looks for a similar solution consider: 1.) Using Joins 2.) Don't be afraid of "sending a few queries through the same connection" :-) 如果有人寻找类似的解决方案,请考虑:1.)使用联接2.)不要害怕“通过同一连接发送一些查询” :-)

Thanks to @dognose and @gordon Linoff! 感谢@dognose和@gordon Linoff!

You need an else clause for the case : 对于这种case您需要一个else子句:

  `tbl_conversations`.`partner_initial_id` =
(CASE WHEN `tbl_conversations`.`user_initial_id` = '30' THEN '30' 
      ELSE `tbl_conversations`.`partner_initial_id` END)

I would also recommend table aliases and explicit JOIN conditions as well. 我还建议使用表别名和显式的JOIN条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM