gpt4 book ai didi

php - 选择最大值和相应的列

转载 作者:行者123 更新时间:2023-11-29 23:59:44 25 4
gpt4 key购买 nike

我试图从更新列中获取最大日期,但也从该表中返回相应的全名,当前发生的情况是返回最新的updates.date,但第一个updates.consultant当前返回为好吧,我们需要 MAX 日期的正确全名。

SELECT customer.id, 
customer.name,
customer.retainer_value,
customer.customer_type,
clientdetails.performance,
clientdetails.url,
members.fullname AS acc_manager,
u.maxdate,
u.fullname
FROM customer
LEFT JOIN clientdetails
ON clientdetails.id = customer.id
LEFT JOIN members
ON members.id = customer.consultant_name
LEFT JOIN (SELECT updates.clientid,
members.fullname,
Max(updates.`date`) AS MaxDate
FROM updates
LEFT JOIN members
ON members.id = updates.consultant
GROUP BY updates.clientid
ORDER BY updates.date DESC) u
ON customer.id = u.clientid
WHERE customer.switchedoff = 'N'
AND customer.companyid <> '3'

最佳答案

我认为您的情况最简单的方法是使用 substring_index()/group_concat() 方法:

SELECT customer.id, 
customer.name,
customer.retainer_value,
customer.customer_type,
clientdetails.performance,
clientdetails.url,
members.fullname AS acc_manager,
u.maxdate,
u.fullname
FROM customer
LEFT JOIN clientdetails
ON clientdetails.id = customer.id
LEFT JOIN members
ON members.id = customer.consultant_name
LEFT JOIN (SELECT updates.clientid,
substring_index(group_concat(m.fullname order by u.date desc separator '|'), '|', 1) as full_name
Max(updates.`date`) AS MaxDate
FROM updates u
LEFT JOIN members m
ON m.id = u.consultant
GROUP BY u.clientid
) u
ON customer.id = u.clientid
WHERE customer.switchedoff = 'N'
AND customer.companyid <> '3' ;

关于php - 选择最大值和相应的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25117131/

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