我先把功能场景简要阐述下: 数据行字段如下: name started_at type 在这张表里,name有重复值 现在需要在type确定的情况下,筛选出一个列表,满足,name不重复,找出的记
我先把功能场景简要阐述下:
数据行字段如下:
name
started_at
type
在这张表里,name有重复值
现在需要在type确定的情况下,筛选出一个列表,满足,name不重复,找出的记录为同name下started_at为最小的记录
举个例子:
活动1 2019-06-01 type1
活动1 2019-06-02 type1
活动1 2019-06-03 type1活动2 2019-06-03 type1
活动2 2019-06-05 type1
活动2 2019-06-07 type1
那筛的列表应为:
活动1 2019-06-01 type1
活动2 2019-06-03 type1
还需要满足started_at 大于 当前时间
请问这样的sql应该如何写?
解决思路为:
就是利用left join 自己
比如s1 left join s2 on s1.name=s2.name and s2.started_at<s1.started_at and s2.started_at > now()
最后where s2.id is null
SELECT
s1.NAME,
s1.started_at,
FROM
tbl s1
LEFT JOIN tbl s2 ON s1.`name` = s2.`name`
AND s1.started_at > s2.started_at
AND s2.started_at > now( )
WHERE
s2.id IS NULL
AND s1.started_at > now( )
AND s1.type = 'online_lecture'
ORDER BY
s1.NAME,
s1.started_at;
--结束END--
本文标题: mysql自联去重的一些笔记记录
本文链接: https://www.lsjlt.com/news/51823.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
2024-05-14
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0