将最后一行连接到另一行

Connect last row to another row

每次客户购买东西时,它都会出现在 "purchases" table 上并获得一个新的 "id"。

我现在想进行一个 mySQL 查询,我可以 运行 在 PHP 上执行此操作:

  1. 选择 "purchases" table 的最后一行,从而识别上次购买的客户。

  2. 然后连接到 "customers" table 并显示该客户的购买次数。

我使用此代码,但它显示了每个客户的购买数量,而不仅仅是我想要的最后一个。

$mysqli = NEW MySQLI ('servername', 'username', 'password', 'dbname');

$resultSet = $mysqli->query ("SELECT quantity FROM customers INNER JOIN purchases ON customers.customer=purchases.customer");

if($resultSet->num_rows != 0){

while($rows = $resultSet->fetch_assoc())
 {   $count = $rows['quantity'];
 echo "<p>Number of purchases: $count";
 }}

试试这个查询:

SELECT quantity FROM customers 
WHERE customer= (select distinct customer 
                from purchases where 
                 id = (select max(b.id) from purchases as b))