gpt4 book ai didi

mysql - 获取每个类别 10 条最新记录,甚至有些记录少于 10 条

转载 作者:行者123 更新时间:2023-11-29 17:45:15 26 4
gpt4 key购买 nike

这是我的表格的示例:

+-----+-----+------------+--------+-------------+--------------+
| LID | AID | Created | TypeID | PaymentDate | PaymentValue |
+-----+-----+------------+--------+-------------+--------------+
| 1 | 529 | 2017-05-12 | 1 | 2017-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 2 | 529 | 2018-04-10 | 4 | 2018-04-10 | 200 |
+-----+-----+------------+--------+-------------+--------------+
| 3 | 441 | 2014-01-23 | 3 | 2014-01-23 | 300 |
+-----+-----+------------+--------+-------------+--------------+
| 4 | 324 | 2017-09-14 | 1 | 2017-09-14 | 400 |
+-----+-----+------------+--------+-------------+--------------+
| 5 | 111 | 2018-05-12 | 0 | 2018-05-12 | 340 |
+-----+-----+------------+--------+-------------+--------------+
| 6 | 529 | 2018-05-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 7 | 529 | 2018-06-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 8 | 529 | 2018-07-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 9 | 529 | 2018-08-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 10 | 529 | 2018-09-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 11 | 529 | 2018-01-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 12 | 529 | 2018-05-14 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 13 | 529 | 2018-05-21 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+
| 14 | 529 | 2018-03-12 | 1 | 2018-05-12 | 100 |
+-----+-----+------------+--------+-------------+--------------+

这是另一个表

+-----+-------+
| ID |caption|
+-----+-------+
| 0 | bad |
+-----+-------+
| 1 | good |
+-----+-------+

我需要为每个 AID 获取 10 条最新记录。如果某些 AID 的记录少于 10 条,我需要获取 10 行,并将“无付款日期”放入 PaymentDate 和 Created 字段中,将 Null 放入 TypeID 中,将 0 放入 PaymentValue 中。我可以获得 10 条或更少的最新记录

select *
from (select *,
(@rn := if(@c = AID, @rn + 1,
if(@c := AID, 1, 1)
)
) as rn
from history cross join
(select @rn := 0, @c := -1) params
order by AID, Created desc
) t
having rn <= 10;

但我不知道如何强制 mysql 为每个 AID 输出 10 行。请帮助我。

结果应采用表格形式
AID、类型Id、已创建、标题

最佳答案

我已经做到了。此查询需要创建一行 10 条记录,以与表中不同的 AID 值组合。我能够显示金额和创建日期的结果,并让您继续,因为您会明白的。关键部分是构建一个包含 10 行乘以不同 AID 的表,因此表 r 中有大约 40 行。然后对表 t 进行左连接,这与您所做的类似。表 t 最多获得 10 条记录的排名。任何缺失的排名最多 10 个建议将由表 r 填充。 Coalesce 将分配默认值,例如金额为 0,日期为“无创建日期”。

http://sqlfiddle.com/#!9/855c21/2

SELECT coalesce(r.aid, t.aid) as aid,
coalesce(t.paymentvalue, 0) as paymentvalue,
coalesce(cast(t.created as char), 'no create date') as created
FROM (select * from (
select 1 as rw union
select 2 union select 3
union select 4 union select 5
union select 6 union select 7
union select 8 union select 9
union select 10) u
cross join (select distinct aid
from history) h
) as r
LEFT JOIN (
SELECT a.aid, a.paymentvalue,
a.created, count(*) rn
FROM history a
JOIN history b
ON a.aid = b.aid
AND a.created <= b.created
GROUP BY a.aid, a.created
HAVING COUNT(*) <= 10) t
on r.rw=t.rn and r.aid=t.aid
order by aid, created;

关于mysql - 获取每个类别 10 条最新记录,甚至有些记录少于 10 条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49847267/

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