MySQL retention of four methods to several decimal places

The SQL LIKE query, there are some special characters to search results need to be converted:

': Searching for wrapping conditions required turned';

%: To place any number of any characters, must be converted to%;

_: Used in place of an arbitrary character, must be converted to _;

: Escape sign, must be converted to \\.

The following are some examples match.

SELECT * FROM `table` WHERE `title` LIKE ‘a’b%’;            — 搜索a’b…

SELECT * FROM `table` WHERE `title` LIKE ‘a%b%’;            — 搜索a%b…

SELECT * FROM `table` WHERE `title` LIKE ‘a_b%’;            — 搜索a_b…

SELECT * FROM `table` WHERE `title` LIKE ‘a\\%’;           — 搜索ab…

In PHP code, this can be replaced by a batch method:

function filterLike($keyword) {

$search = array(”’, ‘%’, ‘_’, ‘\’);

$replace = array(‘\”, ‘\%’, ‘\_’, ‘\\\\’);

return str_replace($search, $replace, $keyword);

}

Published 29 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/HAOXUAN168/article/details/104087346