Andreas Hald
Andreas Hald

Reputation: 215

Syntax Error on INNER JOIN in MySQL

I get a error when trying to inner join databases from a MySqL server.

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

Im Coding in ASP.Net C#.

This is my function that gives the error:

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();

Upvotes: 1

Views: 2256

Answers (1)

Jocelyn
Jocelyn

Reputation: 11393

According to documentation, lines is in the list of MySQL reserved words.

For your query, to work, you must add backticks around the table name:

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

Upvotes: 4

Related Questions