This is the toughest query I ever made:
http://robertr.pastebin.com/X4bG4pFp
"SELECT `user`.`id` , `user`.`fname` , `user`.`lname` ,
YEAR( `user`.`bday` ) AS `bday_year` , `user`.`class_id` ,
( SELECT `class`.`class_name`
FROM `wp_class_classes` `class`
WHERE `user`.`class_id` = `class`.`id`) AS `class_name`
FROM `wp_class_users` `user`
WHERE MONTH( `bday` ) = $month AND DAY( `bday` ) = $day
OR `user`.`fname` =
( SELECT `name`.`names`
FROM `wp_class_namedays` `name`
WHERE `name`.`day` = '$month.$day'
AND `user`.`fname` = `name`.`names` )
This query grabs data from three different database tables to check if there is someone in the database, who has a party today. And in Latvia we have Name Days too. Anyway, this query works well, and does its job well, but now I want to make it a bit cooler.
I want it to show, who will be having a party next week. You’ve probably noticed these emails that Facebook sends to you every weekend showing who has a birthday coming up.
But I just can’t understand how to get at least that interval?
I remember that PHP has some good functions with which you can find on which day starts month and so on, but maybe here are some bright heart, and willing to help me kick me a bit faster forward.
The user table is joined with the name day table, and the result set is then compared against the dates of the current week. The final result set lists only those users whose birthdays or name days happen during the week.
If you want to know about the events of, for example, the next week, you can simply change the intervals in the
week.Datedefinitions as8 - DAYOFWEEK...,9 - DAYOFWEEK...etc.One last thing is, instead of the correlated subquery in the select list you could use
INNER JOIN, like this:The
event_typecolumn as defined above can tell you whether the event is a birthday or not, but it doesn’t let you know whether it’s both the Birthday and a Name Day for that particular person.In case you would like to have that distinction, you could change the
event_typedefinition like this:Now the result of the column would be:
Additionally, you could have
'B'instead of1and'N'instead of2(and''instead of0). The results would be then'B', or'N', or'BN'. Not sure whether+can be used for concatenation, though. If not, put bothCASEs intoCONCAT().