Database date time how to setup

Pop your questions regarding Home automation software here.....
Post Reply
Kroonen
Member
Member
Posts: 302
Joined: Mon Oct 01, 2007 6:38 pm
Location: Netherlands

Database date time how to setup

Post by Kroonen »

Hi,

I setting up differtent databases but i don;t know what is the most smart setting to punt the time on the database, so you can easy do queries

Now on my database i get a unix timestamp but I think it is not handy
I want to selecct on date hour etc. Must i make a table for hours and date seperate?

-------+---------------------+-------+----------+
| dtKey | time | water | verbruik |
+-------+---------------------+-------+----------+
| 114 | 2008-03-04 22:00:02 | 2442 | 9.5 |
| 113 | 2008-03-04 21:00:01 | 2423 | 0.5 |
| 112 | 2008-03-04 20:00:01 | 2422 | 90.0 |
| 111 | 2008-03-04 19:00:01 | 2242 | 67.5 |
| 110 | 2008-03-04 18:00:02 | 2107 | 7.0 |
Digit
Global Moderator
Global Moderator
Posts: 3388
Joined: Sat Mar 25, 2006 10:23 am
Location: Netherlands
Contact:

Database date time how to setup

Post by Digit »

A timestamp is okay.
You can use things like DATEPART, DATEDIFF functions in your queries to manipulate the timestamp field. The mentioned functions are for MSSQL, but i'm sure MySQL has similar things. Just wait for the next reply [:)]
User avatar
Snelvuur
Forum Moderator
Forum Moderator
Posts: 3156
Joined: Fri Apr 06, 2007 11:01 pm
Location: Netherlands
Contact:

Database date time how to setup

Post by Snelvuur »

You can use mtime or ctime. Just like xpl-perl / zenah does. You can also make query's then based on the examples i showed you for the graphs for xpl-perl. This should also be possible by using a different time date.

But your datetime format can be used easily too, because if you want to search on a given date or time, you can always use %like which works easy.

// Erik (binkey.nl)
Kroonen
Member
Member
Posts: 302
Joined: Mon Oct 01, 2007 6:38 pm
Location: Netherlands

Database date time how to setup

Post by Kroonen »

Well i have found this sort of query

SELECT * from digitemp Where time between "2008-03-05 0:00"and "2008-03-05 23:00";

I must look how to work with weeks, but months and years no problem
Bwired
Administrator
Administrator
Posts: 4704
Joined: Sat Mar 25, 2006 1:07 am
Location: Netherlands
Contact:

Database date time how to setup

Post by Bwired »

Code: Select all

(SELECT id, SUM(m3), YEAR(datum) AS jaar, WEEK(datum,5) AS t_week, Round(Sum(M3*0.5319556),2) FROM gas GROUP BY jaar, t_week ORDER BY id DESC limit 30) ORDER BY id ASC
Kroonen
Member
Member
Posts: 302
Joined: Mon Oct 01, 2007 6:38 pm
Location: Netherlands

Database date time how to setup

Post by Kroonen »

Pieter,

You have a time field also on field with such an entry?? Or different?
Bwired
Administrator
Administrator
Posts: 4704
Joined: Sat Mar 25, 2006 1:07 am
Location: Netherlands
Contact:

Database date time how to setup

Post by Bwired »

I have date and time separate but I think that does not matter, the Year and Week function will work.
Post Reply

Return to “Questions & Discussions Forum”