简体   繁体   English

MySQL更新查询-按行号

[英]MySQL Update Query - By Row Number

I'd like to update a row in a MySQL table when a specific row number is reached 当达到特定的行号时,我想更新MySQL表中的行

This is a little confusing since , the real row number of the record isn't a column in the table. 因为,记录的实际行号不是表中的列,所以这有点令人困惑。

And there's no question of iterating over rows , since we're not iterating over an array as in mysql_fetch_array() 毫无疑问,要遍历行,因为我们不像mysql_fetch_array()那样遍历数组mysql_fetch_array()

So , if I'd like to update - say the 3rd row of the table , what would the query be like? 因此,如果我想更新-说表的第三行,查询将是什么样的?

I'm a noob at MySQL 我是MySQL的菜鸟

Thanks a ton for your help ! 多谢您的帮助! :D :D

$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
$query = "SELECT MyColoumn FROM Mytable";
$result = mysqli_query($link, $query);

$row_needed = 3; //Your needed row e.g: 3rd row
for ($i=1,$i=$row_needed,$i++) {
$row = mysqli_fetch_array($result); 
}
// now we are in 3rd row
$query = "UPDATE MyColumn FROM MyTable SET MyColumn = '".$MyColumnUpdate."' WHERE MyColumn = '".$row['MyColumn']."' ";
$result = mysqli_query($link, $query);
...

Try this query 试试这个查询

UPDATE 
   tbl a, 
   (Select 
       @rn:=@rn+1 as rowId, 
       tbl.* 
   from 
       tbl 
   join 
       (select @rn:=0) tmp) b
SET 
   a.columnName = <VALUE>
WHERE 
   b.rowId = <rowNumber> AND
   a.id = b.id;

NOTE The id column must be a unique one, you can use the primary key of that table... 注意 id列必须是唯一的,您可以使用该表的主键...

SQLFIDDLE SQLFIDDLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM