SQL 查询日期范围
让我们再次提出问题——
table1{date, bID, sName, fID}
{11/05,B1,A1,P1}
{12/05,B2,A2,P2}
{13/05,B1,A3,P1}
{15/05,B3,A4,P1}
{16/05,B1,A5,P2}
{19/05,B1,A6,P2}
这是表格,并且还指定了表格中存储的数据...
现在我想要的查询是:-
根据 fId(假设选择了 P1),它应该显示表中的数据为 11/05-17/05(不应错过任何日期)。 检索到的数据如下:-
11/05,B1,A1
12/05,--,--
13/05,B1,A3
14/05,--,--
15/05,B3,A4
16/05,--,--
17/05,--,--
显示针对特定 fID(例如 P1)检索到的数据。解释结果...
1) 它显示了 11/05-17/05 的所有数据,其中 fId 是 P1,如果有数据库中没有日期,那么它也应该显示该日期的空值(即数据库中不存在 14/05 日期,但仍然显示空值)..
2) 如果该特定日期的 fId 不是 P1 ,然后它还在结果集中存储一个空值..
最后在结果集中检索数据,并进一步处理..
所以我想为这个问题编写查询,是否可能..
Lets frame the question again---
table1{date, bID, sName, fID}
{11/05,B1,A1,P1}
{12/05,B2,A2,P2}
{13/05,B1,A3,P1}
{15/05,B3,A4,P1}
{16/05,B1,A5,P2}
{19/05,B1,A6,P2}
This is the table and the data stored in the table is also specified...
Now the query that i want is that:-
Depending of fId (lets say, P1 is selected) it should display the data from table say from 11/05-17/05 (no date should be missed). The data retrieved is as follows:-
11/05,B1,A1
12/05,--,--
13/05,B1,A3
14/05,--,--
15/05,B3,A4
16/05,--,--
17/05,--,--
The data retrieved for a particular fID(say P1) is displayed.. Explaning the result...
1) it displayed all data from 11/05-17/05 where fId is P1, if there is no date in the database, then also it should display null value for that date (i.e.14/05 date was not there in database, but still it displayed with a null value)..
2) if fId for that particular date is not P1, then also it store a null value in result set..
Atlast the data is retrieved in result set,, and processed further..
So i want to write the query for this problemm,, is it possible..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里没有代码,只是我的想法。
您需要创建一个临时表,其中的日期范围从开始日期到结束日期(含)。 然后将
left join
table1
与该临时表放在date
列上,并添加where fID = ?
。No code here, just my thoughts.
You need to create a temporary table with dates ranging from your begin date to an end date, inclusive. And then
left join
table1
with that temporary table ondate
column plus addwhere fID = ?
.正如这里的另一个答案提到的,您需要一个包含所有日期的表和一个 LEFT JOIN 。
假设您有这个表:
然后是这个日期表:
在这个表中您需要拥有要显示的范围内的所有日期。 通常,您会在两个方向上填充几年,但这取决于您。
注意:为了简单起见,我没有考虑两个表中的主键。 当然,您应该确保您有一个主键,对于
dates
表,它可能是dt
列。然后显示您想要的结果:
请注意,仅选择
P1
行是在JOIN
条件中完成的。 如果您添加一个WHERE
子句来执行相同的操作,您将丢失所有没有数据的日期。As the other answer here mentions, a table with all the dates in it, and a
LEFT JOIN
is what you need.Say you have this table:
and then this date-table:
and in this table you need to have all the dates for the range you want to display. Usually you populate it with a couple of years in both directions, but that's up to you.
Note: For simplicity, I did not bother with primary keys in either table. You should of course make sure you have a primary key, and in the case of the
dates
table, it could be thedt
column.Then to display the results you want:
Note that the selection of only
P1
rows is done in theJOIN
criteria. If you add aWHERE
clause to do the same, you'll loose all dates that have no data.