MySQL更新行到最高值加1

时间:2018-01-23 11:26:38

标签: php mysql

这是我的PHP代码段:

$con = mysqli_connect('localhost','root','','db');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"db");
$sql = "UPDATE nations SET queue='MAX(queue) FROM nations' WHERE nation_name='$nation'";
$query = mysqli_query($con,$sql);
$result = mysqli_fetch_assoc($query);

根据我的理解,查询应从列queue获取当前最高值,然后将1添加到该结果,并将 新值插入表中。这不是我想要的方式,它只是将队列设置为0 ...

1 个答案:

答案 0 :(得分:3)

使用

UPDATE nations 
SET queue = (SELECT MAX(queue)+1 FROM nations)
WHERE nation_name = '$nation'