简体   繁体   English

MySQL如何在WHERE中使用两个加密字段更新记录

[英]MySQL How to update a record using two encrypted fields in the WHERE

Not certain this is possible, but I'd like to do this in one step if I can. 不确定是否有可能,但是如果可以的话,我想一步一步做。 I'd like to update field 3, if my passed-in values match two other fields that are encrypted. 如果我传入的值与另外两个加密的字段匹配,我想更新字段3。 Here is what I have that isn't working... 这是我没有用的东西...

UPDATE tbl 
SET field3=CONCAT(field3, 'some additional value') 
WHERE field1 = ENCRYPTION_APPROACH('my_value_in_the_clear', ENCRYPTION_SETTINGS) AND 
field2 = ENCRYPTION_APPROACH('my_other_value_in_the_clear', ENCRYPTION_SETTINGS);

Fields 1 and 2 are NOT keys, but they are programmatically guaranteed to be unique. 字段1和2不是NOT键,但是通过编程保证它们是唯一的。 The encryption appears to be working, I can use the same approach to initially INSERT the record, and then later SELECT it. 加密似乎正在起作用,我可以使用相同的方法首先插入记录,然后再选择它。 However, when I run this with verified test data, I get "0 rows affected", and no error messages. 但是,当我使用经过验证的测试数据运行此程序时,出现“ 0行受影响”,并且没有错误消息。

I can just do a compound call where I first select the record with this same approach, and then update it by it's key (which is working for a different call). 我可以进行复合调用,首先使用相同的方法选择记录,然后通过其键进行更新(这适用于其他调用)。 But I'd really like to keep this to one step if I can. 但是,如果可以的话,我真的很想保持这一步。 Thoughts? 思考?

Aha! 啊哈!

After some looking around, I found the REAL solution. 环顾四周后,我发现了REAL解决方案。 Any time you have a CONCAT and some records are failing, using this little trick... 每当您有CONCAT且某些记录失败时,请使用此小技巧...

UPDATE tbl 
SET field3=IFNULL(CONCAT(field3, 'some additional value'), 'some additional value')
WHERE field1 = ENCRYPTION_APPROACH('my_value_in_the_clear', ENCRYPTION_SETTINGS) AND 
field2 = ENCRYPTION_APPROACH('my_other_value_in_the_clear', ENCRYPTION_SETTINGS);

It wasn't the encryption that was failing... it was when CONCAT was acting on a null field for the first time, it was returning a null. 并不是加密失败了……而是当CONCAT第一次对null字段进行操作时,它返回的是null。 This little trick gets you past the first time you attempt to concat the previous value and works on all subsequent calls! 这个小技巧将使您摆脱第一次尝试连接上一个值并在随后的所有调用中使用的麻烦!

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

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