2012-10-14 82 views
1

嘗試從MySqL服務器內部連接數據庫時出現錯誤。MySQL中INNER JOIN的語法錯誤

我得到這個錯誤:

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 'lines INNER JOIN snippets ON snippets.id = lines.snippetid_fk WHERE lines.snippe' at line 1

即時通訊編碼在ASP.Net C#。

這是我的函數給出了錯誤:

MySqlConnection conn = new MySqlConnection(); 

string mysql = "Server=***;Database=***;User=***;pwd=***"; 
conn.ConnectionString = mysql; 

MySqlCommand cmd = new MySqlCommand(); 
cmd.CommandText = "SELECT * FROM lines " + 
        "INNER JOIN snippets ON snippets.id = lines.snippetid_fk " + 
        "WHERE lines.snippetid_fk = 1"; 
cmd.Connection = conn; 

DataTable dt = new DataTable(); 
MySqlDataAdapter adapter = new MySqlDataAdapter(cmd); 

adapter.Fill(dt); 

Repeater_codebank.DataSource = dt; 
Repeater_codebank.DataBind(); 
+0

你是什麼系的架構和摘要表? snippetid_fk是該表中的一列嗎?也許你需要用方括號包裝所有的對象名稱? 'SELECT * FROM [lines] l INNER JOIN [snippets] s ON(s.id = l.snippetid_fk)WHERE l.snippetid_fk = 1' –

+4

@MohamedNuur - 方括號絕對不會幫助。 MySQL使用反引號。但是你基本上是正確的,'lines'是一個[保留字](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html),所以需要劃界。 –

+1

如果您嘗試直接在查詢窗口中運行SQL,會發生什麼情況? – 5arx

回答

2

根據文檔,lineslist of MySQL reserved words

您的查詢,工作,你必須添加反引號周圍的表名:

cmd.CommandText = "SELECT * FROM `lines` " + 
        "INNER JOIN snippets ON snippets.id = lines.snippetid_fk " + 
        "WHERE `lines`.snippetid_fk = 1";