我有2个表,即customers表和orders表。 基本上我的问题是它总是给我一个错误,这是'未知字段列表'。 我试过在phpmyadmin XAMPP它的工作唯一的问题是当我在VS studio c#中使用它时它给了我一个错误。 我很困惑后面的嘀嗒声'''不起作用,但后面的滴答声在localhost上起作用。 请帮帮我。
这是我的sql语法:
SQL = "SELECT o.`Order ID`, o.Description, o.Amount
FROM tbl_orders AS o
INNER JOIN tbl_customers AS c ON o.`Order ID` = c.`Order ID`
WHERE c.`Customer ID` = '" + cust_id + "'";
cmd.Connection = dbCon;
cmd.CommandText = SQL;
rdr= cmd.ExecuteReader();
dt.Load(rdr);
答案 0 :(得分:1)
尝试删除并重新输入列名周围的`,以防它没有正确复制。还要尝试在字符串之前放一个@。
SQL = @"<the sql>";
这适用于mysql xampp
MySqlConnection sqlConnection = new MySqlConnection(@"Server=localhost;Database=test;");
var sql = @"SELECT o.`Order ID`, o.Description, o.Amount
FROM tbl_orders AS o
INNER JOIN tbl_customers AS c ON o.`Order ID` = c.`Order ID`
WHERE c.`Customer ID` = 1";
sqlConnection.Open();
MySqlCommand sqlCommand = new MySqlCommand(sql,sqlConnection);
sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
答案 1 :(得分:0)
名称中带有空格的列应括在方括号中。
尝试
SQL = "SELECT o.[Order ID], o.Description, o.Amount
FROM tbl_orders AS o
INNER JOIN tbl_customers AS c ON o.[Order ID] = c.[Order ID]
WHERE c.[Customer ID] = '" + cust_id + "'";
cmd.Connection = dbCon;
cmd.CommandText = SQL;
rdr= cmd.ExecuteReader();
dt.Load(rdr);
What is the use of the square brackets [] in sql statements?