4

因为您不能自动增加两列,所以查询将获取表的最后一行和列的 +1 值以插入新行。

例如我有一个名为players

看起来像这样:

id    player_id    player_name
 1      15           name1
 2      16           name2
 3      17           name3

我正在尝试制作一个管理面板来从表单创建播放器,“id”列自动递增,但我也需要 player_id 递增,但 id 和 player_id 彼此不同。

与此类似的查询(我做了这个,以便您更好地理解)

$sql="select top 1 * from `players`  order by id desc";
$result =mysql_query($sql);
while ($data=mysql_fetch_assoc($result)){
    $playerid=$data['player_id'];
    Insert into `players` (`player_id`,`player_name`) VALUE ('$playerid' + '1', 'name4');

}
4

3 回答 3

4

试试这个代码:

    $sql="SELECT player_id FROM `players` ORDER BY id DESC LIMIT 1";
    $result =mysql_query($sql);
    while ($data=mysql_fetch_assoc($result)){
        $playerid=$data['player_id']+1;
       }
mysql_query("Insert into `players` (`player_id`,`player_name`) VALUES ('".$playerid."','name4')");
于 2012-10-20T06:49:45.833 回答
2

以下查询将player_id在插入时分配下一个可用的。

INSERT INTO `players` (`player_id`, `player_name`)
SELECT MAX(`player_id`)+1, 'name4'
FROM `players`;
于 2012-10-20T06:13:53.213 回答
0

试试这个方法

  $sql="SELECT * FROM `players` ORDER BY id DESC LIMIT 1";
    $result =mysql_query($sql);
    $data=mysql_fetch_assoc($result);
    $playerid=$data['player_id']+1;

mysql_query("Insert into `players` (`player_id`,`player_name`) VALUES ('".$playerid."','name4')");
于 2012-10-20T06:07:24.607 回答