base64 在执行 WHERE 之前解码 mysql 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6507385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 20:21:07  来源:igfitidea点击:

base64 decode a mysql column before performing a WHERE

mysqlbase64

提问by Thomas Clayson

Basically - its a long story - but I have a field in a database that is encoded as a base64 string.

基本上 - 这是一个很长的故事 - 但我在数据库中有一个字段被编码为 base64 字符串。

EG: this is stored in the database:

EG:这是存储在数据库中:

YToyOntzOjIwOiJUeXBlX29mX29yZ2FuaXNhdGlvbiI7czoyMDoiTWVtYmVyIG9mIFBhcmxpYW1lbnQiO3M6ODoiUG9zdGNvZGUiO3M6NzoiUEUxIDFKQSI7fQ==

Which equals this:

这等于:

a:2:{s:20:"Type_of_organisation";s:20:"Member of Parliament";s:8:"Postcode";s:7:"#postcode#";}

What I want to be able to do is select where this string LIKE '%Member of Parliament%'. Is there any way to base64 decode a mysql column before performing a WHERE?

我想要做的是选择这个字符串的位置LIKE '%Member of Parliament%'。有什么方法可以在执行之前对 mysql 列进行 base64 解码WHERE

eg: SELECT * FROM table WHERE base64_decode(column) LIKE '%Member of Parliament%'

例如: SELECT * FROM table WHERE base64_decode(column) LIKE '%Member of Parliament%'

Thanks

谢谢

回答by Boy Baukema

If you're using MySQL 5.6.1 or higher you can use the FROM_BASE64() function:

如果您使用 MySQL 5.6.1 或更高版本,则可以使用 FROM_BASE64() 函数:

Takes a string encoded with the base-64 encoded rules used by TO_BASE64() and returns the decoded result as a binary string. The result is NULL if the argument is NULL or not a valid base-64 string. See the description of TO_BASE64() for details about the encoding and decoding rules.

This function was added in MySQL 5.6.1.

mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc')); -> 'JWJj', 'abc'

获取使用 TO_BASE64() 使用的 base-64 编码规则编码的字符串,并将解码结果作为二进制字符串返回。如果参数为 NULL 或不是有效的 base-64 字符串,则结果为 NULL。有关编码和解码规则的详细信息,请参阅 TO_BASE64() 的描述。

这个函数是在 MySQL 5.6.1 中添加的。

mysql> SELECT TO_BASE64('abc'), FROM_BASE64(TO_BASE64('abc')); -> 'JWJj', 'abc'

Otherwise you can use the User Defined Function from: https://github.com/y-ken/mysql-udf-base64

否则,您可以使用来自以下位置的用户定义函数:https: //github.com/y-ken/mysql-udf-base64