我有一个名为“交易”的表,其中存储用户的所有交易。我想要一个 mysql 查询,以便它可以从表中提取特定用户 ID 的最近 3 个事务。 我知道我可以使用限制。
SELECT * FROM 'transactions' WHERE 'userid'=20 LIMIT 0,3;
但是我如何访问查询后作为对象返回的不同事务的属性?还使用限制 0,3 将从表的开头开始搜索,但我想从表的底部开始搜索。我将它与 php 一起使用。
最佳答案
好吧,由于您没有使用编程语言,而只是使用mysql
,因此您的所有属性都将显示在控制台窗口的查询结果中。
编辑:
要访问您的查询和结果,请参阅 PHP.net 网站上的以下文章:http://www.php.net/manual/en/mysqli-result.fetch-assoc.php
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 50,5";
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
}
/* free result set */
$result->free();
}
/* close connection */
$mysqli->close();
?>
/EndEdit
要先对最新的进行排序,假设您有一个 id
列,请使用 ORDER BY
:
SELECT * FROM 'transactions' WHERE 'userid'=20 ORDER BY id DESC LIMIT 0,3;
关于php - 在 mysql 中搜索最近的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17555552/