如何使用 PDO 从最后一个 ID 中选择前一个 ID
How to choose the previous id from the last id with PDO
我想从上一个id中选择上一个id。我该怎么做?
数据库是 mysql.
示例:
id name
1 asd
2 adas
3 ads
4 dsf -> I want to choose this
5 rew
id name ---------------------------------------|
1 asd |
2 adas |
3 ads |
4 dsf |
5 rew -> I want to choose this if I add any "name"
6 zxc
到目前为止我试过这几行:
$sql = $db->prepare("SELECT * FROM answerController ORDER BY id DESC LIMIT 2");
$sql->execute();
$data = $sql->fetch();
您需要提供 offset
作为 limit
的开始。
SELECT * FROM answerController ORDER BY id DESC LIMIT 1, 2
https://dev.mysql.com/doc/refman/8.0/en/select.html
the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)
所以应该是:
$sql = $db->prepare("SELECT name FROM answerController ORDER BY id DESC LIMIT 1, 2");
$sql->execute();
$data = $sql->fetch(PDO::FETCH_ASSOC);
echo $data['name'];
我想从上一个id中选择上一个id。我该怎么做? 数据库是 mysql.
示例:
id name 1 asd 2 adas 3 ads 4 dsf -> I want to choose this 5 rew id name ---------------------------------------| 1 asd | 2 adas | 3 ads | 4 dsf | 5 rew -> I want to choose this if I add any "name" 6 zxc
到目前为止我试过这几行:
$sql = $db->prepare("SELECT * FROM answerController ORDER BY id DESC LIMIT 2");
$sql->execute();
$data = $sql->fetch();
您需要提供 offset
作为 limit
的开始。
SELECT * FROM answerController ORDER BY id DESC LIMIT 1, 2
https://dev.mysql.com/doc/refman/8.0/en/select.html
the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1)
所以应该是:
$sql = $db->prepare("SELECT name FROM answerController ORDER BY id DESC LIMIT 1, 2");
$sql->execute();
$data = $sql->fetch(PDO::FETCH_ASSOC);
echo $data['name'];