2010-10-05 50 views
0

有沒有一種簡單的方法在一個做這兩個查詢:MySQL的選擇下一行不知道ID

$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')); 

感謝

回答

0

子查詢來獲取ID,然後> =所選的ID,然後限制2

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

謝謝,這是完美的......犯了一個錯誤,我的原始查詢,意味着有限制0,1,因此,選擇id從表中WHERE id> =(SELECT ID FROM表WHERE name ='$ name') ORDER BY id ASC LIMIT 0,1會工作。 – Leon 2010-10-05 14:20:12

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));