简体   繁体   English

在指定日期范围内从数据库中选择数据

[英]Selecting data from database within a specified date period

I have a database with employee information and a program, which is supposed to retrieve data from this database. 我有一个包含员工信息和程序的数据库,该程序应该从该数据库中检索数据。

The database structure seems like this: 数据库结构看起来像这样:

ID | Date        | signedIn | SignedOut
1    01-10-2012    08:00:00   16:00:00
1    14-10-2012    08:00:00   16:00:00
1    13-09-2012    08:00:00   16:00:00

When I then try to retrieve data between 01-10-2012 and 15-10-2012 I want go get only the first two rows. 当我再尝试之间检索数据01-10-201215-10-2012我要去拿只有前两排。 So I get a total amount of hours worked to 16. 这样我的总工作时间为16。

But right now I get the total amount to 24, because it also retrieves the last one, even though it is from last month 但是现在我的总数是24,因为它也检索上一个,即使它是上个月的

sql sentence: sql语句:

SELECT Info.UserID, Info.SurName, Info.FirstName, timetabel.Date, 
FROM Info
INNER JOIN timestabel
ON Info.UserID = Bruger_tidstabel.UserID
WHERE Info.UserID = '1'
AND timetabel.Date >= '01-10-2012'
AND timetabel.Date <= '14-10-2012'"

the sentence works, so it can retrieve data from database. 该句子有效,因此它可以从数据库中检索数据。

Is there any way to tell the database, I am working with dates, so it will only retrieve data between two surden dates?? 有什么办法可以告诉数据库,我正在处理日期,因此它将仅检索两个重复日期之间的数据?

Try the BETWEEN Statement, like: 尝试使用BETWEEN语句,例如:

"... WHERE Info.UserID = '1' AND (timetabel.Date BETWEEN '01-10-2012' AND '14-10-2012')"

Check with or without the () .. Also check if the date pattern is correct, depends on your database. 检查是否带()..还要检查日期模式是否正确,取决于您的数据库。

You can use the BETWEEN operator, it is used in a WHERE clause to select a range of data between two values. 您可以使用BETWEEN运算符,该运算符在WHERE子句中用于选择两个值之间的数据范围。

SELECT column_name(s)
 FROM table_name
 WHERE column_name
 BETWEEN value1 AND value2

It is good idea your table field Date to be in date or time stamp format. 最好将表字段“ Date设置为日期或时间戳格式。 Also, the year goes on front of the date format, like this: 此外,年份位于日期格式的前面,如下所示:

SELECT Info.UserID, Info.SurName, Info.FirstName, timetabel.Date, FROM Info INNER JOIN timestabel ON Info.UserID = Bruger_tidstabel.UserID WHERE Info.UserID = '1' AND timetabel.Date BETWEEN '2012-10-01' AND '2012-10-14'

You always need to be careful when representing dates as strings since as Christian says, they are not always treated as expected. 在将日期表示为字符串时,您始终需要小心,因为正如克里斯蒂安所说,它们并不总是被视为预期的。 Ensure you use datetime types or use one of the date functions (DATEADD, DATEDIFF, etc) so that you are comparing dates with dates! 确保使用日期时间类型或使用日期功能之一(DATEADD,DATEDIFF等),以便将日期与日期进行比较! Unfortunately the SQL will often run without error but what it has intepreted from your SQL is not always as expected! 不幸的是,SQL经常可以正常运行,但是从您的SQL中解释的内容并不总是符合预期!

While not answering your question directly (other answered already), I would like to warn you from a common pitfall when working with date ranges. 尽管没有直接回答您的问题(其他人已经回答了),但是在处理日期范围时,我想提醒您一个常见的陷阱。

timetabel.Date <= '14-10-2012'

What happens if the date/time stored in timetabel.Date is 14-10-2012 08:30 ? 如果存储在timetabel.Date的日期/时间是14-10-2012 08:30什么? This date will be excluded. 此日期将被排除。 Personally I prefer to use this safer test 我个人更喜欢使用这种更安全的测试

timetabel.Date < '15-10-2012'

this is equivalent to the above one (except for the time part of cause). 这等效于上述情况(原因的时间部分除外)。 So instead of comparing 所以不要比较

table.column <= date

I compare 我比较

table.column < (date + one day)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM