嗨,我有一个表(transaction_detail),包含来自产品和事务的2个外键,如何选择具有正确transaction_id的最新产品?
在事务表中有(id, transaction_date, total_price, tax等)和在transaction_detail中有(id, transaction_id, product_id, price, qty等)
下面是事务表的示例数据
<表类>
id
transaction_date
total_price
税收
tbody><<tr>6 ab7119c - 0637 4 - e46 b640 e3ee400888cc 2020-01-01 14:00:00 50 0 b18a3a5d - 3 - ce2 - 441 - e - 98 - b5 - ce8e1525ea4c 2020-01-01 15:00:00 60 0 48 bbd11b - 2163 - 4 - a74 ade0 - 52 cdf4a20aa7 2020-01-01 13:30:00 40 0 表类>
你可以试试这个:
select
transaction_id,
product_id,
(select transaction_date from transaction where id=transaction_detail.transaction_id) as transaction_date,
price,
qty
from transaction_detail