闲置天的SQL日期行 [英] SQL Date rows for inactivity days

查看:54
本文介绍了闲置天的SQL日期行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,有一张表存储了每天的访客数量.

Let's say there is this table which stores the number of visitors for each day.

当我要查询表并从中创建图形时,会出现问题.

When I want to query the table and create a graph from it a problem arises.

没有活动的日子在表上没有对应的行.

The days without activity have no corresponding rows on the table.

例如

Day1 - 7
Day2 - 8
Day4 - 7

生成的图形将不正确.由于第3天需要为0.

And the graph generated would not be correct. Since it needs a 0 for Day3.

现在,不使用SQL以外的任何其他方法,是否有可能为非活动日创建这些值?

Now, without using anything other than SQL is it possible to create those values for the inactivity days?

我想创建另一个表,该表将在每次执行脚本时创建30天的所有日期,并且该问题将得到解决,但是我想知道是否有更实用的解决方案.

I thought of creating another table which would create all the dates for the 30days to come each time the scripts gets executed and the problem would have been fixed, but I'm wondering if there is a more practical solution.

谢谢.

推荐答案

您的30天创建表格的解决方案是一种非常简单实用的解决方案.

Your solution of creating a table with the 30 days is a very simple and practical solution.

如果您确实想这样做,可以不用额外的桌子就可以做到,但这并不令人愉快. SQL并非真正允许您选择数据库中不存在的数据.通常,一种更简单的解决方案是在客户端添加丢失的行,而不是尝试编写复杂的SQL语句来完成此操作.

You can do it without an extra table if you really want to, but it's not pleasant. SQL is not really designed to allow you to select data that doesn't exist in your database. A much easier solution in general is to add the missing rows client-side rather than trying to write a complex SQL statement to do this.

这篇关于闲置天的SQL日期行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆