我创建了自己的使用INNER JOINS的sql字符串.这是我的功能:
string cmdstr = @"SELECT s.*,mdb.menu_name FROM stocksdb s,recipelist rl INNER JOIN menudb mdb ON rl.menu_ID = mdb.menu_ID WHERE rl.stock_ID = '" + stockID + "'"; var cmd = new MysqLCommand(cmdstr,db.mycon); var dr = cmd.ExecuteReader(); while (dr.Read()) { var menuname = dr["menu_name"].ToString(); stockIDTxtBox.Text = stockID; nameTxtBox.Text = dr["stock_name"].ToString(); availableTxtBox.Text = dr["stock_quantity"].ToString(); pricePerPieceTxtBox.Text = dr["stock_pricePerPiece"].ToString(); limitTxtBox.Text = dr["stock_limit"].ToString()); recipeList.Items.Add(new ListViewItem(new[] { menuname }); }
现在的问题是,当我点击stocksList ListView上的一个Item时,recipeList ListView将有多个值.像5行左右相同的值.
解决方法
缺少与stocksdb的联接.像这样的东西:
SELECT s.*,mdb.menu_name FROM stocksdb s INNRER JOIN recipelist rl ON s.stock_ID= r1.stock_ID <== this join was missing INNER JOIN menudb mdb ON rl.menu_ID = mdb.menu_ID WHERE rl.stock_ID = '" + stockID