如何查找 mysql 表中缺失的行(日期)?
我尝试过几个这样的主题:如何使用查找丢失的数据行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
因此,我想向用户显示此缺失行表中缺失日期的下拉列表,以便他可以选择他想要提交帖子的日期。
我该怎么做? 谢谢。
I have tried several topics like this one: How to find missing data rows using SQL? here, but I couldn't make it work in my situation.
I have a table named posts
in MySQL which I save user diaries in it every day. Sometimes users forget to write a post for a day and I want to make it possible for them to submit it later.
So the db structures like this:
date userid
2011-10-01 1
2011-10-02 1
(missing)
2011-10-04 1
2011-10-05 1
(missing)
2011-10-07 1
So I want to show a dropdown list of missing dates in this table of missing rows to user, so he can select the date he wants to submit the post for.
How can I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以通过这种方式找到丢失的日期。请注意,您需要删除最后一行,您可以选择适合您的方式来实现这一目标。
You can find the missing date this way. Be careful you need to remove the last row, you can choose a way that suits you to achieve that.
查找缺失日期的最简单方法是使用日历表。我发布了 代码来创建和填充PostgreSQL 的日历表;你应该能够毫无困难地适应它。
有了日历表,您的查询就非常简单且易于理解。要查找 2011 年 10 月的缺失日期,您可以使用类似的方法。 (在你的“帖子”表中猜测。)
The simplest way to find missing dates is to use a calendar table. I've posted code to create and populate a calendar table for PostgreSQL; you should be able to adapt it without any trouble.
With the calendar table in place, your query is pretty simple, and easy to understand. To find the missing dates for October, 2011, you'd use something along these lines. (Guessing at your "posts" table.)
如果您有日期表,这些类型的查询最容易解决。
在您的数据库中,一次性运行此批处理以创建填充日期表。
然后你可以只使用常规的 LEFT JOIN
当然你不想要从 2000 年到 2030 年的所有“缺失”日期。将其限制为 posts 表中的最小和最大日期(对于用户),即
These types of queries are easiest to solve if you have a date table.
In your DB, run this batch as a one-off to create a filled date table.
Then you can just use a regular LEFT JOIN
Of course you don't want all "missing" dates from 2000 to 2030. Limit it to the MIN and MAX dates in the posts table (for the user), i.e.
您可以每次(一天结束)自动输入一个空帖子,带有空标题,无空内容,但实际日期。然后,如果用户想添加前一天的帖子,请显示所有带有空的标题和内容的帖子,然后更新他选择的帖子。
这不是一个空间问题,如果他们写的比错过的要多。例如,如果他们写了4天而错过了1
。如果他们在X天尚未添加丢失的帖子,那么他们可能永远也不会。
如果我的解决方案微不足道 /太抽象,我深表歉意。
You could automatically enter an empty post each time (end of day) with null titles, null contents but actual date. Then, if the user wants to add a post for a previous day, display all the posts with null titles and contents and update the one he selects.
This shouldn't be a space problem, not if they write more than they miss. For instance, if they write for 4 days and miss 1.
Also, you would run a script and delete entries with null titles, null contents AND date older than X days. If they haven't added the missing post for X days, they probably will never do.
I apologize if my solution is trivial / too abstract.