如何按出生日期搜索SQL记录?

huangapple go评论71阅读模式
英文:

How to search SQL records by date of birth?

问题

我有一个SQL表格,其中记录了我的客户的信息,如名字、姓氏、出生日期等等。现在我尝试按出生日期查找所有客户(用于cron,发送假期和生日祝福)。因此,我需要找到所有具有特定出生日期的现有客户。

例如:

在SQL表格中,我有一些记录,其中包含以下出生日期。

+==================+
| Date_of_birthday |
====================
|    1981-06-30    |
--------------------
|    1972-06-30    |
--------------------
|    1966-10-07    |
====================

现在我需要返回所有出生于6月30日的客户。

我尝试过:

SELECT * FROM `table`
WHERE DATE(Date_of_birthday) = '####-06-30';

但我的模式似乎不起作用,它没有返回任何行。

井号字符#应该代表任何数字字符,但它不起作用。

我的错误在哪里?或者我需要以不同的方式编写SQL查询?

英文:

I have a SQL-table where I have records of my clients such as first name, last name, date of birth etc.
Now I try to find all clients by date of birth (for cron, sending holiday and birthday greetings).
Therefore, I need to find all existing clients with a specific date of birth.

For example:

In the SQL table I have a few records with these birth dates.

+==================+
| Date_of_birthday |
====================
|    1981-06-30    |
--------------------
|    1972-06-30    |
--------------------
|    1966-10-07    |
====================

Now I need to return all clients who were born on June 30th.

I tried:

SELECT * FROM `table`
WHERE DATE(Date_of_birthday) = '####-06-30';

But my pattern does not seem to work properly, it doesn't return any lines.

> The hashtag character '#' should represent any numeric character, but it doesn't work.

Where is my mistake? Or do I have to write the SQL query differently?

答案1

得分: 2

使用DATE_FORMAT()函数:

SELECT * FROM `table`
WHERE DATE_FORMAT(Date_of_birthday, '%m-%d') = '06-30';
英文:

With DATE_FORMAT():

SELECT * FROM `table`
WHERE DATE_FORMAT(Date_of_birthday,'%m-%d') = '06-30';

答案2

得分: 2

这个查询应该这样写:

select *
from YourTable
where datepart(day, date_birth) = 30
and datepart(month, date_birth) = 06
英文:

This query should do:

select *
from YourTable
where datepart(day, date_birth) = 30
and datepart(month, date_birth) = 06

答案3

得分: 1

SELECT * FROM table
WHERE right(Date_of_birthday, 5) = '06-30'

英文:
SELECT * FROM `table`
WHERE right(Date_of_birthday, 5) = '06-30'

huangapple
  • 本文由 发表于 2020年1月4日 00:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定