There are some solutions, but i don't know how to change that for my table. So i hope someone could help me.
I have the following Table
âââââââ¦ââââââââââ¦âââââââââââââ¦âââââââââââââ¦ââââââââââââ
â UID â TITLE â BEGIN â END â RECURRING â
â ââââââ¬ââââââââââ¬âââââââââââââ¬âââââââââââââ¬ââââââââââââ£
â 1 â event A â 1359741600 â 1359745200 â none â
â 1 â event B â 1359741600 â 0 â daily â
â 1 â event C â 1359741600 â 0 â weekly â
â 1 â event D â 1359741600 â 0 â monthly â
â 1 â event E â 1359741600 â 0 â yearly â
âââââââ©ââââââââââ©âââââââââââââ©âââââââââââââ©ââââââââââââ
How can i now select every event from now on up to 7 days and also all recurring events in the next 7 days?
The following i've tried but not working very good and not finished.
SELECT
*
FROM
`tx_events_domain_model_event`
WHERE
/* none recurring events in the next 7 days */
(
recuring = 'none'
AND (begin_date + begin_time) >= UNIX_TIMESTAMP(NOW())
AND (end_date + end_time) <= UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 7 DAY))
)
OR
/* Daily */
recuring = 'daily'
OR
/* Weekly */
(
recuring = 'weekly'
AND DAYOFWEEK(NOW()) - 1 <= DAYOFWEEK(FROM_UNIXTIME(begin_date)) - 1
)
OR
/* Monthly */
(recuring = 'monthly'
AND
Here's something I've been playing with (and here it is as an sqlfiddle with some sample data)... not 100% sure about it, but it should grab the last 7 days of data. Note that I'm using MySQL DATETIME versus integer timestamps, but you should be able to convert that easily (for testing the query it was much easier to use string dates).
SELECT *
FROM
(SELECT
*,
CONCAT(YEAR(NOW()), '-', MONTH(NOW()), '-', DAY(start)) AS monthly,
CONCAT(YEAR(NOW()), '-', MONTH(start), '-', DAY(start)) AS yearly
FROM events
) tmp
WHERE
(
(recurring = 'none')
OR (recurring = 'daily')
OR (recurring = 'weekly')
OR (
recurring = 'monthly'
AND (
(
monthly >= NOW()
AND monthly <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
OR (
DATE_ADD(monthly, INTERVAL 1 MONTH) >= NOW()
AND DATE_ADD(monthly, INTERVAL 1 MONTH) <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
)
)
OR (
recurring = 'yearly'
AND (
(
yearly >= NOW()
AND yearly <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
OR (
DATE_ADD(yearly, INTERVAL 1 YEAR) >= NOW()
AND DATE_ADD(yearly, INTERVAL 1 YEAR) <= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
)
)
)
AND start <= NOW()
AND (
end IS NULL
OR end >= DATE_ADD(NOW(), INTERVAL 7 DAY)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With