drs7798 2016-10-30 14:33
浏览 115
已采纳

TIMESTAMPDIFF - 如何在PHP / MySQL中使用它来计算日期值之间的差异

TIMESTAMPDIFF

The link above have helped me how to calculate difference in two dates, however, I need to do the same on a particular column at run time via MySQL select query inside PHP.

For example:

Profile table:

  • Name
  • Gender
  • DOB (Date of Birth)

Running following query via MySQL console, gives exactly the needed result:

SELECT TIMESTAMPDIFF(YEAR, DOB, CURDATE()) AS Age FROM profile;

However, I need to complete the following query to get me the same result combined with other conditions on whole set of DOB values:

SELECT * FROM Profile WHERE Gender='$gn';

I checked Sub-query but that, wont work due to more than one return value.

  • 写回答

1条回答 默认 最新

  • duandiaoqian5795 2016-10-30 15:21
    关注

    As you said, it's the same table, you could use this query:

    SELECT *,TIMESTAMPDIFF(YEAR, DOB, CURDATE()) AS Age
    FROM Profile 
    WHERE Gender='$gn';
    

    Then you'll find an additional field age in your result set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题