MySQL 支援單引號 '...' 與雙引號 "..."(非 ANSI 模式下)包圍字串。
'a string'
"another string"
| 序列 | 含義 | ASCII |
|---|---|---|
\0 | NUL (空字元) | 0 |
\n | 換行 | 10 |
\t | 定位鍵 | 9 |
\r | 歸位 | 13 |
\b | 退格 | 8 |
\' | 單引號 | 39 |
\" | 雙引號 | 34 |
\\ | 反斜線 | 92 |
\% | 百分號 (LIKE 字面值) | 37 |
\_ | 底線 (LIKE 字面值) | 95 |
| 需求 | 寫法範例 | 結果 |
|---|---|---|
| 字串內含單引號 | 'hel''lo' 或 "hel'lo" | hel'lo |
| 字串內含雙引號 | "hel""lo" 或 'hel"lo' | hel"lo |
| 混合引號 | '"hello"' | "hello" |
NUL (ASCII 0) → \0反斜線 (ASCII 92) → \\單引號 (ASCII 39) → \'雙引號 (ASCII 34) → \"建議使用參數化查詢(Prepared Statement)或驅動提供的 escape_string 函數自動處理,避免手工拼接 SQL 導致注入或語法錯誤。
mysql> SELECT 'hello', '"hello"', '""hello""', 'hel''lo', ''hello';
+-------+---------+-----------+--------+--------+
| hello | "hello" | ""hello"" | hel'lo | 'hello |
+-------+---------+-----------+--------+--------+
mysql> SELECT "This\nIs\nFour\nlines";
+--------------------+
| This
Is
Four
lines |
+--------------------+