douxi4114 2013-05-07 15:34 采纳率: 100%
浏览 41

mySQL为多个更新的行添加1到一个数字

Hey all i have the following update for my table in mySQL:

$dbBuilder = '';

foreach($_POST as $key => $value)
{
    if ($value != 'APPROVE') {
        $dbBuilder = $value . ", " . $dbBuilder;
    }
}

$dbBuilder = '(' . rtrim($dbBuilder, ", ") . ')';

UPDATE userCase SET Accepted = 1 WHERE CaseNum in $dbBuilder

Works fine as it should... but now i needed to modify it to allow a new QNum for each of whatever CaseNum.

$result = mysql_query("SELECT QNum FROM userCase ORDER BY QNum DESC limit 1");

while($row = mysql_fetch_assoc($result)) {
    $QNum = $row["QNum"] + 1;
    break;
}

UPDATE userCase SET Accepted = 1, QNum = " . $QNum . " WHERE CaseNum in $dbBuilder

Doing it that way above would set QNum to the same number for all CaseNum in "in". It needs to be unique (as in, adding a 1 to the QNum for each new row).

It only needs to be 1 more than the last QNum in the table. So when someone "approves" a few things (lets say 3 things), it goes to the query above and it needs to be (lets say the last number is 4 in the DB) so it needs to be 5,6,7. The code i have now would just do 5,5,5.

How would i accomplish this using the same CaseNum in.... code above?

Thanks!

  • 写回答

1条回答 默认 最新

  • dongtiran7769 2013-05-07 19:45
    关注
    UPDATE userCase as u1 set Accepted = 1,
        QNum = (select 
            count(*) 
            from userCase as u2 
            where QNum is not null 
            or (
                u2.CaseNum in (...)
                and u2.CaseNum < u1.CaseNum
            ))
    where CaseNum in (...)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值