2011-11-15 125 views
0
update tbl_user set tbl_user.Zoneid='(Select tbl_timezone.Zoneid from tbl_timezone where tbl_timezone.timezone='[UTC - 4:30] Venezuelan Standard Time')' Where tbl_user.userid='1' 

當我運行上面的查詢我得到這個錯誤查詢,在查詢內部在MySQL

您的SQL語法錯誤;檢查與您的MySQL服務器版本相對應的手冊,以找到在'[UTC - 4:30]委內瑞拉標準時間'附近使用的正確語法')'其中tbl_user.userid ='1'在第1行

但是當我執行

update tbl_user set tbl_user.Zoneid='10' Where tbl_user.userid='1'; 

然後正常工作

以下查詢的結果是10

Select tbl_timezone.Zoneid from tbl_timezone where tbl_timezone.timezone='[UTC - 4:30] Venezuelan Standard Time' 

什麼錯我的第一個查詢。爲什麼我會收到錯誤

回答

1

您必須刪除報價:

update tbl_user set tbl_user.Zoneid=(Select tbl_timezone.Zoneid from tbl_timezone where tbl_timezone.timezone='[UTC - 4:30] Venezuelan Standard Time') Where tbl_user.userid='1' 
1

在所有的SQL方言,單引號來分隔字符串:

SELECT this_is_a_column, 'This is a literal string' 
FROM table 

此外,還有是逃逸機制,使你可以寫一個包含單引號的字符串

SELECT 'This string contains \'single\' quotes' 

在你的情況下:

  • 你假裝在一個字符串中寫代碼,因此永遠不會這樣執行。
  • 您在字符串中寫入未加引號的單引號,因此會出現語法錯誤。