条件INSERT mysql查询错误

时间:2012-01-03 22:50:36

标签: php mysql

mysql_query ("INSERT INTO `items` (`item_did_sell`, `sold_for`, `bought_by_paddle`) 
                SELECT ('1','{$sold_for}','{$paddle_number}') 
                FROM `items` WHERE index_number={$index_number}") or die('error!!!' . mysql_error());

我收到此错误:

error!!!Operand should contain 1 column(s)

我不明白为什么。

感谢您的帮助!

编辑:这是用替换为测试值的php变量:

 mysql_query ("INSERT INTO `items` (`item_did_sell`, `sold_for`, `bought_by_paddle`) 
                SELECT ('1','2500','111') 
                FROM `items` WHERE index_number=555") or die('error!!!' . mysql_error());

1 个答案:

答案 0 :(得分:2)

SELECT 语法在列表周围没有括号( )

SELECT列表中删除它们:

mysql_query ("INSERT INTO `items` (`item_did_sell`, `sold_for`, `bought_by_paddle`) 
                SELECT '1','{$sold_for}','{$paddle_number}' 
                FROM `items` WHERE index_number={$index_number}") 
  or die('error!!!' . mysql_error());

另一方面(如我从评论中所理解的),如果您要更新包含index_number={$index_number}的现有行,请使用 UPDATE

mysql_query ("UPDATE `items`  
              SET `item_did_sell` = '1', 
                  `sold_for` = '{$sold_for}',
                  `bought_by_paddle` {$paddle_number}
              WHERE index_number={$index_number}") 
  or die('error!!!' . mysql_error());