tags:

views:

59

answers:

3

I have a MySQL database table, with a unix timestamp field. I am wondering how to get the most recent complete 13 weeks?

Update
But I need to opt out the current week. So I need the last complete(that ended in past or today) 13 weeks.

+2  A: 
WHERE FROM_UNIXTIME(timestamp) > DATE_SUB(FROM_UNIXTIME(timestamp), INTERVAL 13 WEEKS)

If you want to exclude the current week you can do:

WHERE FROM_UNIXTIME(timestamp) < DATE_SUB(NOW(), INTERVAL 1 WEEKS)
halfdan
Could you please help me with another issue: I need to opt out the current week. So I need the last complete(that ended in past or today) 13 weeks.
Pentium10
Added the appropriate query.
halfdan
A: 
SELECT *,FROM_UNIXTIME(time) AS TS FROM MyTable WHERE TS > DATE_SUB(TS, INTERVAL 13 WEEKS)

Think that should work

RobertPitt
He already said it's a unix timestamp..
halfdan
You will get `Unknown column 'TS' in 'where clause'` error! `WHERE` clause can't refer to aliases! You have to use `HAVING` here.
Anpher
Could you please help me with another issue: I need to opt out the current week. So I need the last complete(that ended in past or today) 13 weeks.
Pentium10
A: 

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add

SELECT * FROM foo WHERE created >= DATE_SUB(NOW(), INTERVAL 13 WEEK);
geon
Could you please help me with another issue: I need to opt out the current week. So I need the last complete(that ended in past or today) 13 weeks.
Pentium10