如何使用 mysqli 计算某人今年(以年为单位)的年龄
how to calculate how old someone will turn this year (in years) with mysqli
我想知道有多少 children 今年达到了特定年龄,但我想我需要 mysql 科学家的帮助在这个上。这就是我目前的情况。
$stmt = $mysqli->prepare("SELECT count(YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5))) FROM schs_students where (YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)))=? and gender=1 and house=2");
但它给了我错误的答案。我使用
得到每个 child 的正确年龄
SELECT NOW()-schs_students.dob as age`
但是使用下面的命令给出的计数为零
SELECT count(NOW()-schs_students.dob) FROM schs_students where (NOW()-schs_students.dob)=? and gender=1 and house=2`
dob
在数据库中存储为 DATE
。基本方程是今年 (2015) - 出生年份(例如 2009 给出 6)。
那么为什么第二次计数失败了?或者为什么第一个给出的数字是假的(看起来第一个给出的数字少了一年)?
我使用的完整代码循环如下
for ($i=6;$i<14;$i++)
{
$stmt = $mysqli->prepare("SELECT count(NOW()-schs_students.dob) FROM schs_students where (NOW()-schs_students.dob)=? and gender=1 and house=2");
$stmt->bind_param("i",$i);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_row();
if ($row)
{
$total=$row[0];
echo "<td>{$total}</td>";
//echo $total." of them are {$i} this year.<br/>";
}
$stmt->close();
}
你只需要MySQL中的一个函数,即TIMESTAMPDIFF
:
SELECT TIMESTAMPDIFF(YEAR, '1970-06-01 05:45:33', CURRENT_TIMESTAMP);
现在达到特定年龄的学生人数 今天:
SELECT count(*) FROM schs_students where
TIMESTAMPDIFF(YEAR, schs_students.dob, CURRENT_TIMESTAMP)=? and gender=1 and house=2
显然,schs_students.dob
需要 DATETIME
而不仅仅是 char
。
今年 年 达到特定年龄的学生人数会有点困难......实际上,我想截止日期是 12 月 31 日,所以使用那个日期:
SELECT count(*) FROM schs_students where
TIMESTAMPDIFF(YEAR, schs_students.dob, concat(Year(NOW()),'-12-31'))=? and gender=1 and house=2
我想知道有多少 children 今年达到了特定年龄,但我想我需要 mysql 科学家的帮助在这个上。这就是我目前的情况。
$stmt = $mysqli->prepare("SELECT count(YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5))) FROM schs_students where (YEAR(CURRENT_TIMESTAMP) - YEAR(dob) - (RIGHT(CURRENT_TIMESTAMP, 5) < RIGHT(dob, 5)))=? and gender=1 and house=2");
但它给了我错误的答案。我使用
得到每个 child 的正确年龄SELECT NOW()-schs_students.dob as age`
但是使用下面的命令给出的计数为零
SELECT count(NOW()-schs_students.dob) FROM schs_students where (NOW()-schs_students.dob)=? and gender=1 and house=2`
dob
在数据库中存储为 DATE
。基本方程是今年 (2015) - 出生年份(例如 2009 给出 6)。
那么为什么第二次计数失败了?或者为什么第一个给出的数字是假的(看起来第一个给出的数字少了一年)?
我使用的完整代码循环如下
for ($i=6;$i<14;$i++)
{
$stmt = $mysqli->prepare("SELECT count(NOW()-schs_students.dob) FROM schs_students where (NOW()-schs_students.dob)=? and gender=1 and house=2");
$stmt->bind_param("i",$i);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_row();
if ($row)
{
$total=$row[0];
echo "<td>{$total}</td>";
//echo $total." of them are {$i} this year.<br/>";
}
$stmt->close();
}
你只需要MySQL中的一个函数,即TIMESTAMPDIFF
:
SELECT TIMESTAMPDIFF(YEAR, '1970-06-01 05:45:33', CURRENT_TIMESTAMP);
现在达到特定年龄的学生人数 今天:
SELECT count(*) FROM schs_students where
TIMESTAMPDIFF(YEAR, schs_students.dob, CURRENT_TIMESTAMP)=? and gender=1 and house=2
显然,schs_students.dob
需要 DATETIME
而不仅仅是 char
。
今年 年 达到特定年龄的学生人数会有点困难......实际上,我想截止日期是 12 月 31 日,所以使用那个日期:
SELECT count(*) FROM schs_students where
TIMESTAMPDIFF(YEAR, schs_students.dob, concat(Year(NOW()),'-12-31'))=? and gender=1 and house=2