MySQL 查询和间隔

发布于 2024-10-19 12:54:00 字数 923 浏览 1 评论 0原文

这是我做过的最难的查询: 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` )

此查询从三个不同的数据库表中获取数据以检查是否数据库中有一个人今天举办了聚会。在拉脱维亚,我们也有命名日。不管怎样,这个查询运行良好,并且完成了它的工作,但现在我想让它更酷一点。

我想让它显示谁下周将举办聚会。您可能已经注意到 Facebook 每个周末都会向您发送这些电子邮件,其中显示谁的生日即将到来。

但我只是不明白如何至少得到那个间隔?

我记得PHP有一些很好的功能,你可以用它们找到月份的哪一天开始等等,但也许这里有一些聪明的心,愿意帮助我更快地前进。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还不是爱你 2024-10-26 12:54:00
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

用户表与姓名日表连接,然后将结果集与当前周的日期进行比较。最终结果集仅列出生日或命名日发生在一周内的用户。

例如,如果您想了解下周的事件,只需将 week.Date 定义中的间隔更改为 8 - DAYOFWEEK...9 - DAYOFWEEK... 等。

最后一件事是,您可以使用 INNER JOIN 代替选择列表中的相关子查询,如下所示:

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 */

上面定义的 event_type 列可以告诉您该事件是否是生日,但它不会让您知道该事件是否既是该特定人的生日又是命名日。

如果您希望进行这种区分,您可以更改 event_type 定义,如下所示:

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`

现在该列的结果将是:

  • 1 – 生日
  • 2 – 命名日
  • 3 – 两者

另外,您可以用 'B' 代替 1'N' 代替 2 (以及 '' 而不是 0)。结果将是 'B''N''BN'。但不确定 + 是否可以用于连接。如果不是,请将两个 CASE 放入 CONCAT() 中。

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().

年少掌心 2024-10-26 12:54:00

我不确定我的查询是否正确,但是 MySQL 文档。您可能想尝试以下操作:(

... where date = SYSDATE() + 7

检查语法,我来自 Oracle;))

这将使各方在接下来的 7 天内保持联系。

I'm not sure if I get your query right, but the command SYSDATE() is mentioned in the MySQL docs. You might want to try something like:

... where date = SYSDATE() + 7

(check the syntax, I come from Oracle ;) )

This will get the parties for the next 7 days.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文