MySQL 查询和间隔
这是我做过的最难的查询: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
用户表与姓名日表连接,然后将结果集与当前周的日期进行比较。最终结果集仅列出生日或命名日发生在一周内的用户。
例如,如果您想了解下周的事件,只需将
week.Date
定义中的间隔更改为8 - DAYOFWEEK...
、9 - DAYOFWEEK...
等。最后一件事是,您可以使用
INNER JOIN
代替选择列表中的相关子查询,如下所示:上面定义的 event_type
列可以告诉您该事件是否是生日,但它不会让您知道该事件是否既是该特定人的生日又是命名日。如果您希望进行这种区分,您可以更改
event_type
定义,如下所示:现在该列的结果将是:
另外,您可以用
'B'
代替1
和'N'
代替2
(以及''
而不是0
)。结果将是'B'
、'N'
或'BN'
。但不确定+
是否可以用于连接。如果不是,请将两个CASE
放入CONCAT()
中。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 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_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:Now the result of the column would be:
Additionally, you could have
'B'
instead of1
and'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 bothCASE
s intoCONCAT()
.我不确定我的查询是否正确,但是 MySQL 文档。您可能想尝试以下操作:(
检查语法,我来自 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:(check the syntax, I come from Oracle ;) )
This will get the parties for the next 7 days.