2013-07-09 81 views
0

我有一个名为'transactions'的表,其中存储了用户的所有交易。我想要一个MySQL查询,这样它会从表中为特定的用户标识提取最近的3个事务。 我知道我可以使用限制。在mysql中搜索最近?

SELECT * FROM 'transactions' WHERE 'userid'=20 LIMIT 0,3;

但我怎么能访问这些查询后返回的对象不同交易的属性?也使用限制0,3将开始从表的开始搜索,但我想从表的底部开始搜索。我正在使用它与PHP。

回答

1

好吧,既然你没有使用编程语言,并且只有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; 
+0

我正在使用它与PHP。 –

+0

阅读http://www.php.net/manual/en/mysqli-result.fetch-assoc.php –