dqw7121 2014-01-28 05:51
浏览 98
已采纳

MySQL查询 - 日期字段的周年纪念

I am setting up a mysql database with 2 fields one is a name (text string) and one with a date. yyyy-mm-dd.

I need to query the table to show only the names that match the dd-mm. So on the anniversary of that date every year those names get printed.

$query = "SELECT * FROM FRSLNamelist WHERE $dateofdeath"; 
$result = mysql_query($query); 
while ($line = mysql_fetch_assoc($result)) { 
echo $line['fullname'];
echo "<br>";

I can't find anywhere how to do this. I know that you can explode the date field separated so you only have to match the month and day.

I need the function to do:

If [datefield] = [current.day.month] then print [name].

anybody have quick fix.

Thanks in Advance.

  • 写回答

1条回答 默认 最新

  • douketangyouzh5219 2014-01-28 05:56
    关注

    If the stored date is in format yyyy-mm-dd as you mentioned, and you have to ignore the yyyy part then one way would be:

    $dateOfdeath=date("m-d");
    $query = "SELECT * FROM FRSLNamelist WHERE  dateField LIKE '%-$dateofdeath'"; 
    

    Another, even better, way would be:

    $d=date("d");
    $m=date("m");
    $query = "SELECT * FROM FRSLNamelist WHERE MONTH(dateField)=$m AND DAY(dateField)=$d"; 
    

    Edit:

    with the dateFieldName, could i use curdate?

    Yes

    SELECT * FROM FRSLNamelist 
    WHERE MONTH(dateField)=MONTH(CURDATE())
    AND DAY(dateField)=DAY(CURDATE())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序