gpt4 book ai didi

mysql - 如何获得唯一 ID 和 MAX(日期)?

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:48 26 4
gpt4 key购买 nike

有人可以帮助我吗?我不知道我认为是正确的查询有什么问题..

项目表 item

贷款表
loan

典当 table
pawner

贷款分配表 loan_assignment

这是我获取唯一 ID 和 MAX(date) 的查询

SELECT distinct p.pawner_id, c.item_name, c.description, l.net_proceeds, 
max(DATE_FORMAT(a.date_loan_granted, '%d-%b-%Y')) as date
from pawner p, loan l, collateral c, loan_assignment a, pawnshop b
WHERE b.pawnshop_id = a.pawnshop_id AND p.pawner_id = a.pawner_id
AND l.loan_id = a.loan_id AND a.item_id = c.item_id
AND b.pawnshop_id = 1 group by p.pawner_id;

上述^查询的结果是这样的: enter image description here

我喜欢发生的结果是下面这个 enter image description here

最佳答案

根据评论,如果您想要每个 pawner_id 的最高 date_loan_granted 值,而不是每个 (pawner_id, item_id),您可能需要:

select m.pawner_id,
i.item_name,
i.description,
l.net_amount,
date_format(m.date_loan_granted, '%d-%b-%Y') as date_loan_granted
from (select pawner_id,
max(loan_id) as loan_id,
max(date_loan_granted) as date_loan_granted
from loan_assignment
group by pawner_id) m
join loan_assignment la
on m.pawner_id = la.pawner_id
and m.loan_id = la.loan_id
and m.date_loan_granted = l.date_loan_granted
join loan l
on m.loan_id = l.loan_id
join item i
on la.item_id = i.item_id

关于mysql - 如何获得唯一 ID 和 MAX(日期)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31368692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com