我有多个交易的受益人。在主页上,我想显示客户创建的最新交易。因此,最新交易的受益人应该获得金额详情。 我创建了这样的查询。但它并没有显示最新的交易。
$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
";
答案 0 :(得分:0)
也许这有效:
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
请注意,我刚在where子句中添加了一个子选择:AND tran.date_added = (SELECT MAX(date_added) FROM transaction WHERE ben_id = b.id)
答案 1 :(得分:0)
我找到了解决方案。如果有人需要,我会发布答案,而不是删除问题。
$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
";
我仍然不知道,它现在怎么样了。我之前也试过ORDER BY date_added DESC
。但是,它没有用。无论如何,它现在正在运作。 :)