tags:

views:

1230

answers:

4

Hi,

I have this SQL query about getting the 5 events today:

SELECT n.nid, n.type, n.title, nr.body, nr.teaser, FROM_UNIXTIME(e.event_start) start_date, FROM_UNIXTIME(e.event_end) end_date
FROM node n
LEFT JOIN event e ON n.nid = e.nid
LEFT JOIN node_revisions nr ON nr.nid = e.nid
WHERE n.`type` = 'event'
AND NOW() BETWEEN FROM_UNIXTIME(e.event_start) AND FROM_UNIXTIME(e.event_end)
ORDER BY n.`created` DESC
LIMIT 5

Then I need to get the "this week's event" using "a week that includes "today" and starts on a Sunday".

How can I do that in MySQL?

Any help would greatly be appreciated. Thanks in advance :)

Cheers, Mark

+7  A: 

You need to define "this week" better -- do you mean a 7-days sliding window centered on today, or a week (the one that includes "today") starting e.g. on a Sunday? That's entirely dependent on the semantics of "this week" and it's impossible for us to decide what you meant by said ambiguous expression. Of the two approaches you mention, one or the other (or a variant thereon) will be appropriate depending on your meaning.

Edit: the OP has clarified in a comment that he means "a week that includes "today" and starts on a Sunday" -- and I deduce from his use of FROM_UNIXTIME that the specific SQL dialect he's targeting is MySQL. Then, WEEK(somedate, 0) is the MySQL function that should give him exactly what he wants, see mysql's docs.

Specifically,

AND WEEK(CURDATE, 0) BETWEEN WEEK(FROM_UNIXTIME(e.event_start), 0)
                         AND WEEK(FROM_UNIXTIME(e.event_end), 0)

should be the WHERE clause the OP is looking for.

Alex Martelli
Hi Alex, sorry for my imcomplete question because I am not sure as well but I confirmed it and the correct one is "a week that includes "today" and starts on a Sunday" :)
marknt15
Thanks a lot Alex! :D It worked. I am not familiar with the WEEK() function until now. It works now. Cheers :)
marknt15
Great answer, Alex
Christian
@Christian, glad you liked it! @marknt15, always happy to help.
Alex Martelli
Um Alex, I have a follow up question. I am trying to get all records for this month. I have this query but doesn't work. What do you think the error is?SELECT n.nid, n.type, n.title FROM node nWHERE n.`type` = 'event'AND CURDATE() <= LAST_DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH)) AND CURDATE() > DATE_ADD(LAST_DAY(CURDATE()), interval 1 day)ORDER BY n.`created` DESCLIMIT 5Thanks again.
marknt15
How can today be less than a month ago or more than tomorrow? That's what you're testing, not anything about the database... three times curdate in the WHERE clause.
Alex Martelli
+1  A: 

I'm not sure if this is for SQL Server or MySQL, but in MySQL you could get the current weekday of today and then use date_add to subtract that many days from the current date (start date) then using start date, use date_add again to add 7 days (end date).

Hopefully that helps, let me know if you need help with the syntax.

Christian
A: 

Based on the table/column names, it appears you're working with Drupal. Have you considered using a View to achieve your goal? I can't tell from the context whether this is part of a module you're writing, in which case keep plugging away, or whether you just want a list of events to display in a block, in which case a View should be able to do all this for you without messing around with PHP/SQL.

dave
A: 

I don't know if you have that option, but for performance reasons it could be better to do the date calculations in your program code. If you use a function on a column in a WHERE clause, MySQL cannot use indexes. A simple example: http://netfactory.dk/2004/12/13/mysql-date-functions-and-indexes/ Most languages should have decent functions/libraries for date/time manipulation.

Marie Fischer