gpt4 book ai didi

MySQL 选择每个客户的最新调查分数

转载 作者:行者123 更新时间:2023-11-29 03:39:16 24 4
gpt4 key购买 nike

简单来说,我有一个如下所示的表格:

customer_surveys
----------------
id | customer_id | score | date
1 | 1 | 5 | 01-01-2013
2 | 1 | 6 | 01-02-2013
3 | 2 | 8 | 01-03-2013
4 | 2 | 7 | 01-04-2013

我想创建一个如下所示的 View :

customer_survey_scores
----------------------
customer_id | score
1 | 6
2 | 7

该 View 仅选择每个客户的最新调查分数。

我能够通过编写一个复杂的子查询来实现这一点,但需要它尽可能快地执行。

最佳答案

select c.customer_id, c.score
from customer_surveys c
inner join
(
select customer_id, max(date) as mdate
from customer_surveys
group by customer_id
) x on x.customer_id = c.customer_id and x.mdate = c.date

关于MySQL 选择每个客户的最新调查分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16701516/

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