gpt4 book ai didi

mysql - 如何在 MySQL 中转义特殊字符?

转载 作者:IT老高 更新时间:2023-10-28 12:53:32 26 4
gpt4 key购买 nike

例如:

select * from tablename where fields like "%string "hi"  %";

错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'hi" "' at line 1

如何构建此查询?

最佳答案

The information provided in this answer can lead to insecure programming practices.

The information provided here depends highly on MySQL configuration, including (but not limited to) the program version, the database client and character-encoding used.

http://dev.mysql.com/doc/refman/5.0/en/string-literals.html

MySQL recognizes the following escape sequences.\0     An ASCII NUL (0x00) character.\'     A single quote (“'”) character.\"     A double quote (“"”) character.\b     A backspace character.\n     A newline (linefeed) character.\r     A carriage return character.\t     A tab character.\Z     ASCII 26 (Control-Z). See note following the table.\\     A backslash (“\”) character.\%     A “%” character. See note following the table.\_     A “_” character. See note following the table.

So you need

select * from tablename where fields like "%string \"hi\" %";

虽然是 Bill Karwin notes below , 对字符串分隔符使用双引号不是标准 SQL,因此使用单引号是一种很好的做法。这简化了事情:

select * from tablename where fields like '%string "hi" %';

关于mysql - 如何在 MySQL 中转义特殊字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/881194/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com