场景

 需要每天为过生日的同学发送祝福

涉及技术

 golang:基础语言
 postgresql:  关系型数据库

问题

怎样去postgre数据库中筛选过今天生日的同学,里面存在birthday字段,以及身份证号字段

解决方法

1.根据身份证号分析后几位月日进行模糊查询进行匹配。

结果:不可行,模糊查询容易检测出错误的数据

2.使用postgresql的复杂查询筛选出指定日期的所有人员信息

结果:可行
实验过程1:
select * from student where birthday between to_date('2015-07-01','YYYY-MM-DD') and to_date('2025-08-15','YYYY-MM-DD');
查询指定日期内的数据
如果改成 select * from student where birthday between to_date('07-01','MM-DD') and to_date('08-15','MM-DD');
不可用,查询不到数据

实验过程2:
select to_char((SELECT now()::timestamp),'mm-dd') 获取当前时间的月份,进行修改
select to_char(((SELECT birthday from student where stunumber=1715925105)::timestamp),'mm-dd')
结果只能查到1715925105的这个人出生日期的月日

实验过程3:
select * from student where to_char(birthday,'mm-dd')='08-26' 查询日期为08-26,成功

Last modification:August 22nd, 2020 at 04:06 pm
如果觉得我的文章对你有用,请随意赞赏