2

这是我的代码:

string cmdstr = "UPDATE itemsordered i " +
                "INNER JOIN"+
                    "(  SELECT itemsOrdered_quantity, itemsOrdered_ID,  "+
                            "IF itemsOrdered_quantity = '"+quantityTxtBox.Text+"' SELECT 'EQUAL' "+
                            "IF itemsOrdered_quantity < '" + quantityTxtBox.Text + "' SELECT 'LESS' " +
                            "IF itemsOrdered_quantity > '" + quantityTxtBox.Text + "' SELECT 'MORE'  " +
                        "END AS r "+
                        "FROM itemsordered "+
                    ") res ON i.itemsOrdered_ID = res.itemsOrdered_ID "+
               "INNER JOIN stocksdb s ON s.stock_ID = i.stock_ID " +
               "SET s.stock_quantity = (s.stock_quantity + i.itemsOrdered_quantity), " +
                   "s.stock_dateUpdated = '" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "', " +
                   "i.itemsOrdered_status = 'RECEIVED', " +
                   "i.itemsOrdered_dateReceived = '" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "'" +
                   "WHERE i.itemsOrdered_ID = '" + idTxtBox.Text + "' AND res.r = 'EQUAL'";
cmd = new MySqlCommand(cmdstr, db.mycon);
cmd.ExecuteNonQuery();

MessageBox.Show("ITEM RESTOCKED!");

我真正想做的是,如果itemsOrdered_quantity等于 中的值,quantityTxtBox那么它将s.stock_quantity通过将 上的金额添加itemsOrdered_quantity到当前金额来更新 。

我还想制作其他 IF 子句。但我想先解决这个难题。

它返回一个错误cmd.ExecuteNonQuery并指向我的 SQL 字符串“靠近”我的 IF 语句。

4

3 回答 3

1

MySQL 为此使用CASE语句,基本语法是:

SELECT CASE WHEN a = b THEN c ELSE d END CASE

你也可以做

SELECT CASE WHEN a = b THEN c 
            WHEN a > b THEN e
            ELSE d END CASE
于 2013-01-10T12:30:57.817 回答
0
select (case when  itemsOrdered_quantity= '"+quantityTxtBox.Text+"'then 'EQUAL' else (case when itemsOrdered_quantity<  '"+quantityTxtBox.Text+"' then 'LESS' else  'MORE' end ) end) as r from itemsordered 
于 2013-01-10T12:53:49.953 回答
0

是的,你可以使用if 语句

   SELECT IF(1<2,'yes','no');
   //output  -> 'yes'
  //if 1<2 is true then return  `yes` if not true return `no`
于 2013-01-10T12:39:15.567 回答