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
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_UNIXTIMEthat 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,
should be the
WHEREclause the OP is looking for.