Editing
排除 MySQL 技術疑難問題
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== SQL 查詢語法錯誤 == === 捕獲異常:SQLSTATE[HY093]:無效的參數編號:參數未定義 === 解決方案 * 問號的數量與查詢值的數量不一致 <ref>[https://stackoverflow.com/questions/10966251/sqlstatehy093-invalid-parameter-number-parameter-was-not-defined php Invalid parameter number: parameter was not defined - Stack Overflow]</ref> * 查詢值的數組不應該是關聯數組。使用順序數組! === 錯誤!:SQLSTATE[HY000]:一般錯誤 === 訊息:錯誤!:SQLSTATE[HY000]:一般錯誤 狀況:當我使用 [http://php.net/manual/en/book.pdo.php PHP: PDO] 解決方案: * "您不應該在 update 或 insert 查詢中使用 {{kbd | key=<nowiki>$result = $stmt->fetchAll();</nowiki>}}" <ref>[https://stackoverflow.com/questions/12979510/pdo-error-sqlstatehy000-general-error-when-updating-database php - PDO error: " SQLSTATE[HY000]: General error " When updating database - Stack Overflow]</ref>。 * 您不應該在查詢 {{kbd | key=<nowiki>INTO OUTFILE ...</nowiki>}} 中使用 {{kbd | key=<nowiki>$result = $stmt->fetchAll();</nowiki>}} <ref>[http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/ Save MySQL query results into a text or CSV file]</ref><ref>[https://stackoverflow.com/questions/13369164/cant-create-write-to-file-errcode-22 mysql - Can't create/write to file (Errcode: 22) - Stack Overflow]</ref>。 === 錯誤:ASCII '\0' 出現在語句中,但這是不允許的,除非啟用了 --binary-mode 選項並且 mysql 以非互動模式運行 === 訊息:{{kbd | key=<nowiki>錯誤:ASCII '\0' 出現在語句中,但這是不允許的,除非啟用了 --binary-mode 選項並且 mysql 以非互動模式運行。如果期望 ASCII '\0',請將 --binary-mode 設為 1。查詢:''。</nowiki>}} 狀況:當我導入 sql 文件時遇到上述錯誤訊息。 解決方案:解壓縮文件然後再次導入文件<ref>[https://stackoverflow.com/questions/17158367/enable-binary-mode-while-restoring-a-database-from-an-sql-dump mysql - Enable binary mode while restoring a Database from an SQL dump - Stack Overflow]</ref>。sql 文件是壓縮文件。您可以使用 [https://en.wikipedia.org/wiki/File_(command) file (command)] 來識別文件的類型。 <pre> $ file compressed.sql compressed.sql:gzip 壓縮數據 $ file plain_text.sql plain_text.sql:UTF-8 Unicode 文本,帶有非常長的行 </pre> === 錯誤 1055: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column === 訊息 <pre> 查詢發生錯誤 (1055): Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.posts.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by </pre> 遇到錯誤的查詢 <pre> -- 建立範例資料表 CREATE TABLE posts ( id INT, post_id VARCHAR(10), author VARCHAR(50), content TEXT, likes INT, post_time DATETIME ); -- 插入範例資料 INSERT INTO posts VALUES (1, 'A123', 'Alice', '第一篇文章', 10, '2024-01-01'), (2, 'A123', 'Bob', '推推', 5, '2024-01-02'), (3, 'A123', 'Bob', '讚讚', 3, '2024-01-03'); -- 這會報錯 SELECT id, -- 問題: 沒有在 GROUP BY 中 post_id, author, GROUP_CONCAT(content SEPARATOR "\n") AS content, -- 問題: 沒有在 GROUP BY 中 likes -- 問題: 沒有在 GROUP BY 中 FROM posts GROUP BY post_id, author; </pre> 解決方案:修正後的查詢 (1) SELECT 中的每個欄位要麼放在 GROUP BY 中、或 (2) 使用彙總函數 (如 MAX, MIN, SUM, COUNT, GROUP_CONCAT) <pre> -- 方法一:取消 `sql_mode=only_full_group_by`: 有兩種方法可以取消 `sql_mode=only_full_group_by`: 1. 暫時性修改 (僅對當前會話有效) ```sql SET sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY','')); ``` 2. 永久性修改 (修改 MySQL 設定檔) - 找到 MySQL 設定檔 (通常是 `my.cnf` 或 `my.ini`) - 在 `[mysqld]` 區段加入或修改: ```ini [mysqld] sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION ``` 注意事項: 1. 修改設定檔後需要重啟 MySQL 服務才會生效 2. 不建議取消這個設定,因為: - 可能導致查詢結果不可預測 - 違反 SQL 標準 - 可能在未來版本中產生相容性問題 建議的做法是修正 SQL 查詢而不是更改 SQL mode。 -- 方法二: 把所有欄位加入 GROUP BY SELECT id, post_id, author, content, likes FROM posts GROUP BY post_id, author, id, content, likes; -- 方法三: 對非 GROUP BY 的欄位使用彙總函數 SELECT MAX(id) as id, post_id, author, GROUP_CONCAT(content) as contents, SUM(likes) as total_likes FROM posts GROUP BY post_id, author; </pre> === 錯誤 1235: 這個版本的 MySQL 尚未支持 'LIMIT & IN/ALL/ANY/SOME 子查詢' === 訊息:錯誤 1235 (42000):這個版本的 MySQL 尚未支持 'LIMIT & IN/ALL/ANY/SOME 子查詢'。 解決方案:將子查詢的語法更改為 {{kbd | key =column_name BETWEEN start_number to end_number}} 進一步閱讀: * [https://dev.mysql.com/doc/mysql-reslimits-excerpt/5.6/en/subquery-restrictions.html MySQL :: MySQL 限制和局限 :: 4 子查詢的限制] === 錯誤 1267: 字元集混合不合法 (utf8mb4_general_ci,COERCIBLE) 與 (latin1_swedish_ci,IMPLICIT) 用於 'concat' 操作 === 錯誤情況:嘗試合併不同類型的數據,例如 CONCAT(string, int) 解決方案:{{kbd | key =<nowiki>SELECT CONCAT(`string_column`, CONVERT(`int_column`, CHAR))</nowiki>}} 或 {{kbd | key =<nowiki>SELECT CONCAT(`string_column`, CAST(`int_column` AS CHAR))</nowiki>}}<ref>[https://stackoverflow.com/questions/15368753/cast-int-to-varchar mysql - 將 int 轉換為 varchar - Stack Overflow]</ref><ref>[http://blog.51cto.com/bian5399/1092772 mysql字元集問題:不合法的字元集混合 - 51CTO部落格]</ref> === 錯誤 1305 - 函數 MY_TABLE.MY_FUNCTION 不存在 === 訊息:MySQL 錯誤 1305 - 函數 MY_TABLE.MY_FUNCTION 不存在 解決方案:修正函數名稱中的打字錯誤 === 錯誤 1690 - BIGINT UNSIGNED 值超出範圍 === 訊息:MySQL 錯誤 #1690 (BIGINT UNSIGNED 值超出範圍) 解決方案 <ref>[https://dev.mysql.com/doc/refman/8.0/en/out-of-range-and-overflow.html MySQL :: MySQL 8.0 參考手冊 :: 11.2.6 超出範圍和溢出處理]</ref><ref>[https://stackoverflow.com/questions/34115917/mysql-error-1690-bigint-unsigned-value-is-out-of-range-for-unix-timestamp MySQL 錯誤 #1690 (BIGINT UNSIGNED 值超出範圍) 用於 UNIX_TIMESTAMP() - Stack Overflow]</ref>: <pre> SET sql_mode = 'NO_UNSIGNED_SUBTRACTION'; </pre> === 錯誤 1366: 字串值不正確 === 訊息:SQLSTATE[HY000]: 一般錯誤:1366 字串值 '\xF0\x9F\x87\xAF\xF0\x9F...' 對於欄位 'XXX' 不正確 解決方案: * 檢查 PHP PDO 的字元集。並執行 {{kbd | key=set names utf8mb4}}<ref>[https://stackoverflow.com/questions/54947392/incorrect-string-value-when-trying-to-pass-emoji-to-the-db-encoded-with-utf8mb4 php - 嘗試將使用 utf8mb4 編碼的表情符號傳遞給資料庫時字串值不正確 - Stack Overflow]</ref> * 檢查表的配置 ** 字元集 (字元集、字元集): {{kbd | key=utf8mb4}} ** 排序規則 (定序、字元序): {{kbd | key=utf8mb4_unicode_ci}} === 錯誤!: SQLSTATE[42000]: 語法錯誤或存取違規 === 訊息:{{kbd | key=<nowiki>錯誤!: SQLSTATE[42000]: 語法錯誤或存取違規:1064 您在 SQL 語法中有錯誤;請檢查手冊,了解您的 MySQL 伺服器版本對應的正確語法以用於 ...</nowiki>}} 解決方案: * [[SQL syntax debug | SQL 語法除錯]] * [https://www.eversql.com/sql-syntax-check-validator/ SQL 語法檢查在線上,SQL 驗證器,即時 SQL 編譯器在線上 - EverSQL] {{exclaim}} 不支持 [https://www.php.net/manual/en/pdo.prepare.php PHP: PDO::prepare],其中查詢語法包含問號。
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information