mysql在不知道id的情况下选择下一行

时间:2010-10-05 14:09:15

标签: php mysql

有一种简单的方法可以在一个中执行这两个查询:

$name = 'somename';

$id = mysql_result(mysql_query('SELECT id FROM table WHERE name = $name'));

$next_id = mysql_result(mysql_query('SELECT id FROM table WHERE id > $id ORDER BY id ASC LIMIT 1, 1'));

由于

2 个答案:

答案 0 :(得分:0)

获取ID的子查询,然后> =所选ID,然后限制2

SELECT id FROM table WHERE id >= (SELECT id FROM table WHERE name = '$name') ORDER BY id ASC LIMIT 2

答案 1 :(得分:0)

你试过吗?

$next_id = mysql_result(mysql_query('SELECT id FROM table WHERE id > (SELECT id FROM table WHERE name = $name LIMIT 1,1) ORDER BY id ASC LIMIT 1, 1));