[英]Get latest record from multiple for each user
I have beneficiary which have multiple transactions. 我有多个交易的受益人。 On home page I want to show the latest transaction created by the customer.
我要在主页上显示客户创建的最新交易。 So, latest transaction's beneficiary should come on top with amount details.
因此,最新交易的受益人应放在金额详细信息之上。 I have created query something like this.
我已经创建了类似这样的查询。 But it doesn't show the latest transaction.
但是它不会显示最新的交易。
$clause = "SELECT b.*, tran.transfer_amount, tran.date_added as tran_date_added
FROM beneficiary b
LEFT JOIN customer_beneficiary_mapping cbm ON b.id = cbm.ben_id
LEFT JOIN
(SELECT ben_id, transfer_amount, date_added
FROM transaction
GROUP BY ben_id
) tran
ON tran.ben_id = b.id
INNER JOIN system_country_list scl ON scl.country_id = b.ben_country
AND cbm.cus_id = '$cus_id'
ORDER BY tran.date_added DESC
";
maybe this works: 也许这可行:
SELECT b.*, tran.transfer_amount, tran.date_added as tran_date_added
FROM beneficiary b
LEFT JOIN customer_beneficiary_mapping cbm ON b.id = cbm.ben_id
LEFT JOIN
(SELECT ben_id, transfer_amount, date_added
FROM transaction
GROUP BY ben_id
) tran
ON tran.ben_id = b.id
AND tran.date_added = (SELECT MAX(date_added) FROM transaction WHERE ben_id = b.id)
INNER JOIN system_country_list scl ON scl.country_id = b.ben_country
AND cbm.cus_id = '$cus_id'
ORDER BY tran.date_added DESC
Note that i just added a subselect in where clause: AND tran.date_added = (SELECT MAX(date_added) FROM transaction WHERE ben_id = b.id)
请注意,我只是在where子句中添加了一个子选择:
AND tran.date_added = (SELECT MAX(date_added) FROM transaction WHERE ben_id = b.id)
I found the solution. 我找到了解决方案。 Rather than deleting the question, I am posting the answer if somebody needs.
如果有人需要,我会发布答案,而不是删除问题。
$clause = "SELECT b.*, tran.transfer_amount, tran.date_added as tran_date_added
FROM beneficiary b
LEFT JOIN customer_beneficiary_mapping cbm ON b.id = cbm.ben_id
LEFT JOIN
(SELECT ben_id, transfer_amount, date_added
FROM transaction
GROUP BY ben_id
ORDER BY 1 DESC <----- Added this line
) tran
ON tran.ben_id = b.id
INNER JOIN system_country_list scl ON scl.country_id = b.ben_country
AND cbm.cus_id = '$cus_id'
ORDER BY tran.date_added DESC
";
I still don't know, how come it's working now. 我仍然不知道,它现在如何运作。 I also tried with
ORDER BY date_added DESC
before. 我之前也尝试过使用
ORDER BY date_added DESC
。 But, it was not working. 但是,它没有用。 Anyway, It's working now.
无论如何,它现在正在工作。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.