英文:
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'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论