用单个mysql表的多语句更新多条记录?

时间:2013-04-11 02:02:13

标签: php mysql sql

我试图用多更新语句更新mysql表。
我尝试了我做多选的方式,但它给出了错误。

  UPDATE  (
    UPDATE resources SET resources.resourcecount = resources.resourcecount - '100'
    WHERE resourcetype = '1'
    ),
    (
        UPDATE resources SET resources.resourcecount = resources.resourcecount - '250'
    WHERE resourcetype = '2'
    ),
    (
        UPDATE resources SET resources.resourcecount = resources.resourcecount - '300'
    WHERE resourcetype = '3'
    ),
        (
        UPDATE resources SET resources.resourcecount = resources.resourcecount - '600'
    WHERE resourcetype = '4'
    )

我不知道是否有可能通过查询本身或者我应该使用PHP循环吗?

2 个答案:

答案 0 :(得分:1)

如果您只是在resourcetype为1,2,3或4时减去100,则可以执行以下操作:

UPDATE resources
  SET resources.resourcecount = resources.resourcecount - 100
  WHERE resourcetype IN ('1', '2', '3', '4')

答案 1 :(得分:0)

$updateSql = "UPDATE resources SET resources.resourcecount = resources.resourcecount - '100'
    WHERE resourcetype = '1';";

$updateSql .= "UPDATE resources SET resources.resourcecount = resources.resourcecount - '100'
    WHERE resourcetype = '2';";

mysqli_multi_query($link,$updateSql);