如何使用sql从数据库的日期列中导出年龄?

时间:2019-03-28 15:59:56

标签: mysql mysqli

我正在尝试使用mysqli显示处于特定年龄范围(例如10到20岁)的所有用户。哪种查询最适合推算年龄,如何查询?

我目前在查询中出现问题,无法显示准确的结果。我想要的是显示介于此年龄与该年龄之间的用户,但是下面的查询没有给出任何错误,而是显示甚至低于10或高于20的年龄。我有2个表user和user_likes。

        user
        username(pk)   gender   date_of_birth      country
          John           M        2010-12-31        India
          Bing           M        2000-12-31        Italy
          Rose           F        2000-12-01         UK

         user_likes 
         username(fk)    gender     age_from      to_age
         John             F             18            30
         Bing             F             20            30
         Rose             M             18            25

下面的$username是存储在该变量中的会话。

        $sql = "SELECT gender, age_from, to_age FROM user_likes WHERE username = '$username'";
        $result = $conn->query($sql);
        if ($result->num_rows > 0)
        {
            while ($row = $result->fetch_assoc())
            {
                $gender1 = $row['gender'];
                $age_from = $row['age_from'];
                $to_age = $row['to_age'];
            }
        }

        $sql1 = "SELECT username, date_of_birth, TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age FROM user WHERE NOT username = '$username' AND gender = '$gender1'  AND (date_of_birth BETWEEN '$age_from' AND '$to_age')";
        $result = $conn->query($sql1);
        if ($result->num_rows > 0)
        { 

            while ($row = $result->fetch_assoc())
            {                   
                echo "<div>";
                    echo "<span><a href = 'accounts.php?usernames=".$username."'><img src = '".$row['photo']."' alt = 'profile photo'></span>";
                    echo "<div>".$row['username']."</a><br><span>From".$row['country']."</span></div>";
                echo "</div>";
            }               
        }

上面的代码中的问题是,如果我从18到80之间选择年龄,即使18岁以下或80岁以上的年龄仍然显示,所以我认为这与

有关
      TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS age

      date_of_birth BETWEEN '$age_from' AND '$to_age'

我期望的是-假设我是Rose,并且由于我的兴趣性别是男性,并且所选的年龄范围是18到25,所以我只希望显示'Bing'个用户。

2 个答案:

答案 0 :(得分:1)

date_of_birth BETWEEN '$age_from' AND '$to_age'

让我们用一些值替换字段。

'2010-12-31' BETWEEN '18' AND '20'

这是胡说八道,您可能想使用

...
AND TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN '$age_from' AND '$to_age'

例如:

模式(MySQL v5.7)

CREATE TABLE users (
  `username` VARCHAR(4),
  `gender` VARCHAR(1),
  `date_of_birth` VARCHAR(10),
  `country` VARCHAR(5)
);

INSERT INTO users
  (`username`, `gender`, `date_of_birth`, `country`)
VALUES
  ('John', 'M', '2010-12-31', 'India'),
  ('Bing', 'M', '2000-12-31', 'Italy'),
  ('Rose', 'F', '2000-12-01', 'UK');

查询#1

SELECT username,
       date_of_birth,
       TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS "age"
FROM users
WHERE (TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN '18' AND '20');

输出

| username | date_of_birth | age |
| -------- | ------------- | --- |
| Bing     | 2000-12-31    | 18  |
| Rose     | 2000-12-01    | 18  |

查询#2

SELECT username,
       date_of_birth,
       TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) AS "age"
FROM users
WHERE (TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN '01' AND '10');

输出

| username | date_of_birth | age |
| -------- | ------------- | --- |
| John     | 2010-12-31    | 8   |

View on DB Fiddle

答案 1 :(得分:1)

从我的角度来看,您的查询存在缺陷。在

CREATE INDEX x_mutable_start_time ON mutable (name, start_time DESC);

...您正在将...WHERE ... AND (date_of_birth BETWEEN '$age_from' AND '$to_age') (日期)与date_of_birth(整数)进行比较。这没有任何意义,您可以获得任何意外的结果。

尝试修改WHERE子句,以便:

age

最后一句:

...WHERE ... AND (TIMESTAMPDIFF(YEAR, date_of_birth, CURDATE()) BETWEEN '$age_from' AND '$to_age')