gpt4 book ai didi

mysql - 如何在 mysql 表中查找缺失的行(日期)?

转载 作者:可可西里 更新时间:2023-11-01 07:49:15 26 4
gpt4 key购买 nike

我试过几个这样的主题:How to find missing data rows using SQL?在这里,但我无法让它在我的情况下工作。

我在 MySQL 中有一个名为 posts 的表,我每天都在其中保存用户日记。有时用户忘记写一天的帖子,我想让他们稍后提交。所以数据库结构是这样的:

date           userid
2011-10-01 1
2011-10-02 1
(missing)
2011-10-04 1
2011-10-05 1
(missing)
2011-10-07 1

所以我想在这个缺失行表中向用户显示一个缺失日期的下拉列表,这样他就可以选择他想要提交帖子的日期。

我该怎么做?谢谢。

最佳答案

查找缺失日期的最简单方法是使用日历表。我已经发布了 code to create and populate a calendar table for PostgreSQL ;你应该能够毫无困难地适应它。

有了日历表,您的查询就非常简单,也很容易理解。要查找 2011 年 10 月缺失的日期,您将使用类似的方法。 (在您的“帖子”表中猜测。)

select c.cal_date
from calendar c
left join posts p on (c.cal_date = p.date)
where p.date is null
and c.cal_date between '2011-10-01' and '2011-10-31'
and p.userid = 1
order by c.cal_date

关于mysql - 如何在 mysql 表中查找缺失的行(日期)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5522701/

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