Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4013896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:25:44+00:00 2026-05-20T09:25:44+00:00

This is the toughest query I ever made: http://robertr.pastebin.com/X4bG4pFp SELECT `user`.`id` , `user`.`fname` ,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:25:45+00:00Added an answer on May 20, 2026 at 9:25 am
    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`,
      CASE
        WHEN MONTH(`week`.`Date`) = MONTH(`user`.`bday`) AND
             DAY(`week`.`Date`) = DAY(`user`.`bday`) THEN 1
        ELSE 2
      END AS `event_type`
    FROM  `wp_class_users`  `user`
      LEFT JOIN  `wp_class_namedays`  `name` ON  `user`.`fname` =  `name`.`names`
      LEFT JOIN  (
        SELECT CURDATE() + INTERVAL (1 - DAYOFWEEK(CURDATE())) DAY AS `Date`  UNION ALL
        SELECT CURDATE() + INTERVAL (2 - DAYOFWEEK(CURDATE())) DAY  UNION ALL
        SELECT CURDATE() + INTERVAL (3 - DAYOFWEEK(CURDATE())) DAY  UNION ALL
        SELECT CURDATE() + INTERVAL (4 - DAYOFWEEK(CURDATE())) DAY  UNION ALL
        SELECT CURDATE() + INTERVAL (5 - DAYOFWEEK(CURDATE())) DAY  UNION ALL
        SELECT CURDATE() + INTERVAL (6 - DAYOFWEEK(CURDATE())) DAY  UNION ALL
        SELECT CURDATE() + INTERVAL (7 - DAYOFWEEK(CURDATE())) DAY
      ) `week`
        ON CONCAT(MONTH(`week`.`Date`), '.', DAY(`week`.`Date`)) IN (
          CONCAT(MONTH(`user`.`bday`), '.', DAY(`user`.`bday`)),
          `name`.`day`
        )
    WHERE `week`.`Date` IS NOT NULL
    

    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.Date definitions as 8 - DAYOFWEEK..., 9 - DAYOFWEEK... etc.

    One last thing is, instead of the correlated subquery in the select list you could use INNER JOIN, like this:

    SELECT
      `user`.`id`,
      `user`.`fname`,
      `user`.`lname` ,
      YEAR(`user`.`bday`) AS  `bday_year`,
      `user`.`class_id`,
      `class`.`class_name`
    FROM  `wp_class_users`  `user`
      INNER JOIN  `wp_class_classes`  `class` ON  `user`.`class_id` =  `class`.`id`
      LEFT JOIN  `wp_class_namedays`  `name` ON  ...  /* the rest of the above script */
    

    The event_type column 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_type definition like this:

    CASE
      WHEN MONTH(`week`.`Date`) = MONTH(`user`.`bday`) AND
           DAY(`week`.`Date`) = DAY(`user`.`bday`) THEN 1
      ELSE 0
    END +
    CASE CONCAT(MONTH(`week`.`Date`), '.', DAY(`week`.`Date`))
      WHEN `name`.`day` THEN 2
      ELSE 0
    END AS `event_type`
    

    Now the result of the column would be:

    • 1 – a birthday
    • 2 – a name day
    • 3 – both

    Additionally, you could have 'B' instead of 1 and 'N' instead of 2 (and '' instead of 0). The results would be then 'B', or 'N', or 'BN'. Not sure whether + can be used for concatenation, though. If not, put both CASEs into CONCAT().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is one of the toughest things I have ever tried to do. Over
I was reading about this person's interview at a well-known search company. http://asserttrue.blogspot.com/2009/05/one-of-toughest-job-interview-questions.html He
this is dankogai's javascript deflate http://github.com/dankogai/js-deflate I can't inflate from c#,please help me
this is by far my toughest question yet and I'm hoping someone has stumbled
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
This is my first post here and I wanted to get some input from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.