因为你不能自动递增两列,查询将获得表的最后一行,列的+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');
}
答案 0 :(得分: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')");
答案 1 :(得分:2)
以下查询将在插入时分配下一个可用的player_id
。
INSERT INTO `players` (`player_id`, `player_name`)
SELECT MAX(`player_id`)+1, 'name4'
FROM `players`;
答案 2 :(得分: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')");