Php 如何更新行数

Php 如何更新行数,php,mysql,Php,Mysql,这应该是一个很简单的问题,但我想不出来。在mysql表中,有两列是questionNumber和rowNumber。我想通过问题编号更新行编号顺序。这是我的php,问题出在查询(“更新问题集行号=($x=$x+1)按问题号排序”)。有人帮我修吗 <?php $link = mysqli_connect($dbhost, $username, $dbpass, $database); if (!$link) { echo "Error: Unable to

这应该是一个很简单的问题,但我想不出来。在mysql表中,有两列是
questionNumber
rowNumber
。我想通过
问题编号
更新
行编号
顺序。这是我的php,问题出在查询
(“更新问题集行号=($x=$x+1)按问题号排序”)
。有人帮我修吗

<?php

    $link = mysqli_connect($dbhost, $username, $dbpass, $database);

    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }

    $x = 0;
    $sql = "UPDATE Question SET rowNumber = ($x=$x+1) ORDER BY QuestionNumber";

    if ($link->query($sql) === TRUE) {
        echo "Updated";
    } else {
        echo "Error updating record: " . $link->error;
    }
    $link->close();
    ?>

也许您可以不用
$x
就可以完成,请尝试以下操作:

UPDATE t1 Question t1
INNER JOIN (
    SELECT @rowno := @rowno + 1 AS rowno, QuestionNumber
    FROM Question
    CROSS JOIN (SELECT @rowno := 0) t
    ORDER BY QuestionNumber
) t2
ON t1.QuestionNumber = t2.QuestionNumber
SET t1.rowNumber = t2.rowno

这是我刚刚拼凑的一个视频。想象一个包含城市和州的表,以及一个rownum列

我想更新rownum列,但只更新state=South Carolina的行。。。SC

我希望更新顺序是按城市名称。数据最初是按物理顺序插入的,以显示其工作原理,即SC城市名称最初不是按字母顺序插入的

模式:

drop table if exists locat123;
create table locat123
(   id int auto_increment primary key,
    city varchar(100) not null,
    state varchar(100) not null,
    rownum int not null
);
insert locat123 (city,state,rownum) values
('a1','NY',-1),('a2','NY',-1),('a3','NY',-1),('a4','NY',-1), 
('m1','MT',-1),('m2','MT',-1),
('s8','SC',-1),('s2','SC',-1),('s4','SC',-1),('s1','SC',-1),('s11','SC',-1);
update locat123 l
join
(   select l.id,l.city,@rn:=@rn+1 as rown
    from locat123 l
    cross join (select @rn:=0) params
    where l.state='SC' -- <==================== right there, update SC only
    order by l.city -- By the way, 5 rows that are South Carolina (SC) in here
) xDerived
on l.id=xDerived.id
set l.rownum=xDerived.rown;
-- 5 rows updated
select * from locat123 order by state,city;

+----+------+-------+--------+
| id | city | state | rownum |
+----+------+-------+--------+
|  5 | m1   | MT    |     -1 |
|  6 | m2   | MT    |     -1 |
|  1 | a1   | NY    |     -1 |
|  2 | a2   | NY    |     -1 |
|  3 | a3   | NY    |     -1 |
|  4 | a4   | NY    |     -1 |
| 10 | s1   | SC    |      1 |
| 11 | s11  | SC    |      2 |
|  8 | s2   | SC    |      3 |
|  9 | s4   | SC    |      4 |
|  7 | s8   | SC    |      5 |
+----+------+-------+--------+
带有派生表的Update语句:

drop table if exists locat123;
create table locat123
(   id int auto_increment primary key,
    city varchar(100) not null,
    state varchar(100) not null,
    rownum int not null
);
insert locat123 (city,state,rownum) values
('a1','NY',-1),('a2','NY',-1),('a3','NY',-1),('a4','NY',-1), 
('m1','MT',-1),('m2','MT',-1),
('s8','SC',-1),('s2','SC',-1),('s4','SC',-1),('s1','SC',-1),('s11','SC',-1);
update locat123 l
join
(   select l.id,l.city,@rn:=@rn+1 as rown
    from locat123 l
    cross join (select @rn:=0) params
    where l.state='SC' -- <==================== right there, update SC only
    order by l.city -- By the way, 5 rows that are South Carolina (SC) in here
) xDerived
on l.id=xDerived.id
set l.rownum=xDerived.rown;
-- 5 rows updated
select * from locat123 order by state,city;

+----+------+-------+--------+
| id | city | state | rownum |
+----+------+-------+--------+
|  5 | m1   | MT    |     -1 |
|  6 | m2   | MT    |     -1 |
|  1 | a1   | NY    |     -1 |
|  2 | a2   | NY    |     -1 |
|  3 | a3   | NY    |     -1 |
|  4 | a4   | NY    |     -1 |
| 10 | s1   | SC    |      1 |
| 11 | s11  | SC    |      2 |
|  8 | s2   | SC    |      3 |
|  9 | s4   | SC    |      4 |
|  7 | s8   | SC    |      5 |
+----+------+-------+--------+
那么为什么是派生表呢?因为我们必须引入一个变量作为计数器递增。我们使用
交叉连接
的唯一目的是将该变量引入整个过程。在解析派生表之后,我们将其结果折叠到普通的
更新中,并使用一个连接
模式对其进行包装

当然,正如用户FirstOne所说,我们可以使用
Update。。。在某些情况下,按
订购。以上是我为这一个想到的


哦,只是重申一下,派生表通常用于清理我们定制的信息,并将其折叠到查询的大部分中。

UPDATE。。订购人
?那该怎么办?是的。我有两列,并且
questionNumber
总是在变化。我还想根据
问题编号
更改
行编号
。Thanke.idk在派生表上使用联接进行更新,该派生表受益于order by。你为什么要这样做,据说是这样的。在你查询之前做x=$x+1,只需按如下方式进行查询:更新问题集rowNumber=$xAbdullah,@FirstOne和我询问原因,是因为你可能正在将数据(rownum)保存在一个表中,而其中包含“可视顺序号”将变得陈旧、无用或容易出错。我们想了解一下原因。你也是老板……给你加了+1。当我在SC的某个地方的时候,他实际上专注于他的东西。把它放在一个存储的进程中(一个你梦寐以求的进程,就像@10086中的答案)或者一个你制作的进程中。您可以传递参数。存储的进程将保护您的
@rn
变量或他的
@rowno
var)。。。否则,请参阅此链接:……好的,该链接是我花了一秒钟找到它,因为它是在一个旧问题上/我最近编辑的。那么,看看“Revision1(PHP显示)”部分和Heredoc(一个块
),如果我们向您展示的内容有效(@10086和I),但它在PHP中不起作用,那么它很可能是或可能是一个var问题。存储过程可以保护您免受此问题的影响,或者多查询或PDO等效问题。但是,我只会使用存储过程,但在任何情况下,请找个时间加入我的聊天室。如果我不在,请Ping我(我的键盘将发出嘟嘟声)。您可能遇到了我在您的问题的主要评论中提到的陈旧数据备份问题。没什么大不了的,我们可以解决它。在去掉第一个t1后,这是sql的完美工作,但对于php它不工作。它使我的所有数字列为0。