問題描述
例如:
select * from tablename where fields like "%string "hi" %";
錯誤:
<塊引用>您的 SQL 語法有錯誤;檢查與您的 MySQL 服務器版本相對應的手冊,了解在第 1 行的 'hi" "' 附近使用的正確語法
如何構建此查詢?
此答案中提供的信息可能會導致不安全的編程實踐.
此處提供的信息高度依賴于 MySQL 配置,包括(但不限于)程序版本、數據庫客戶端和使用的字符編碼.
見http://dev.mysql.com/doc/refman/5.0/en/string-literals.html
<前>MySQL 識別以下轉義序列.\0 一個 ASCII NUL (0x00) 字符.\' 單引號('")字符.\" 雙引號 (""") 字符.\b 退格字符.\n 換行(換行)字符.\r 回車符.\t 一個制表符.\Z ASCII 26 (Control-Z).請參閱表格后面的注釋.\\ 反斜杠(\")字符.\% 一個角色.請參閱表格后面的注釋.\_ 一個角色.請參閱表格后面的注釋.所以你需要
select * from tablename where fields like "%string \"hi\";%";
盡管 Bill Karwin 在下面說明,對字符串分隔符使用雙引號不是標準 SQL,因此使用單引號是一種很好的做法.這簡化了事情:
select * from tablename where '%string hi"之類的字段%';
For example:
select * from tablename where fields like "%string "hi" %";
Error:
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
How do I build this query?
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.
See 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\" %";
Although as Bill Karwin notes below, using double quotes for string delimiters isn't standard SQL, so it's good practice to use single quotes. This simplifies things:
select * from tablename where fields like '%string "hi" %';
這篇關于如何在 MySQL 中轉義特殊字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!