每当顾客购买某物时,它就会出现在"购买"表并获得一个新的" id"。
我现在想要创建一个mySQL查询,我可以在PHP上运行并执行此操作:
选择"购买"的最后一行表并因此识别最后一次购买的客户。
然后连接到"客户"表并向我显示该客户的购买数量。
我使用此代码,但它显示了每个客户的购买数量,而不仅仅是我想要的最后一个。
$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";
}}
&#13;
答案 0 :(得分:1)
尝试此查询:
SELECT quantity FROM customers
WHERE customer= (select distinct customer
from purchases where
id = (select max(b.id) from purchases as b))