gpt4 book ai didi

MySQL:获取前 1 个 ID

转载 作者:行者123 更新时间:2023-11-29 04:35:20 25 4
gpt4 key购买 nike

我想弄清楚如何为每个与客户 ID 相关联的客户 ID 选择第一个属性 ID。请帮忙。我该如何查询?

PropertyID  ClientID    CustomerID  Date
10 1 35 2004
20 1 35 2004
30 2 35 2004
40 2 35 2004
50 3 35 2004
60 3 35 2004
70 4 35 2004
80 4 35 2004
90 5 35 2004
100 5 35 2004
110 6 35 2005
120 6 35 2005
130 7 35 2005
140 7 35 2005
150 8 35 2005
160 8 35 2005
170 9 35 2005
180 9 35 2005
220 15 37 2007
240 15 37 2007
260 16 37 2007
270 16 37 2007

预期结果:

PropertyID   ClientID   CustomerID
10 1 35
30 2 35
50 3 35
70 4 35
90 5 35
110 6 35
130 7 35
150 8 35
170 9 35
220 15 37
260 16 37

最佳答案

假设 1st 你的意思是最低的 propertyId,你可以在子查询中使用聚合来找到每个 clientId 的最低 propertyId,然后将结果与原始表连接以获得其他相应的列。

select propertyId, clientId, customerId
from your_table t
join (
select clientId,
min(propertyId) as propertyId
from your_table
group by clientId
) t2 using (clientId, propertyId);

这假设 propertyId 是唯一的(至少每个客户端)。

Demo

关于MySQL:获取前 1 个 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007415/

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