计算 datediff 时的差异 MySQL
Difference when calculating datediff MySQL
我在一个案例和一个地方使用 datediff 时得到不同的结果。
这是 table 用户,省略了不相关的列,所有用户:
<table><tbody><tr><th>UserID</th><th>UserTypeID</th><th>UserRegistrationDate</th><th>UserBirthDate</th><th>UserJobID</th><th>UserLocationID</th></tr><tr><td>1</td><td>1</td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>2</td><td>1</td><td>3/12/2015</td><td>30/1/1982</td><td>2</td><td>3</td></tr><tr><td>3</td><td>1</td><td>2/12/2015</td><td>19/9/1987</td><td>2</td><td>4</td></tr><tr><td>12</td><td>1</td><td>2/12/2015</td><td>11/9/1992</td><td>2</td><td>5</td></tr><tr><td>13</td><td>1</td><td>13/12/2015</td><td>15/5/1995</td><td>2</td><td>2</td></tr><tr><td>40</td><td>1</td><td>10/1/2016</td><td>22/7/1975</td><td>2</td><td>2</td></tr><tr><td>47</td><td>1</td><td>16/1/2016</td><td>29/2/2000</td><td>1</td><td>6</td></tr><tr><td>50</td><td> </td><td> </td><td>1/12/1956</td><td>2</td><td>2</td></tr><tr><td>52</td><td> </td><td> </td><td>12/3/1998</td><td>2</td><td>6</td></tr><tr><td>54</td><td>1</td><td>20/1/2016</td><td>11/4/1990</td><td>2</td><td>2</td></tr><tr><td>55</td><td>1</td><td>25/1/2016</td><td> </td><td> </td><td> </td></tr><tr><td>57</td><td> </td><td> </td><td>1/1/1990</td><td> </td><td> </td></tr><tr><td>58</td><td>1</td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>59</td><td>1</td><td>28/1/2016</td><td> </td><td> </td><td> </td></tr><tr><td>60</td><td>1</td><td>13/1/2016</td><td>1/1/1998</td><td>1</td><td>10</td></tr><tr><td>61</td><td>1</td><td>1/12/2015</td><td>22/6/1989</td><td>1</td><td>12</td></tr><tr><td>66</td><td>1</td><td>1/2/2016</td><td> </td><td> </td><td> </td></tr><tr><td>67</td><td>1</td><td>1/2/2016</td><td> </td><td> </td><td> </td></tr><tr><td>68</td><td>1</td><td>1/2/2016</td><td> </td><td> </td><td> </td></tr></tbody></table>
当运行宁此:
SELECT
COUNT(users.UserID),
CASE WHEN users.UserBirthDate IS NULL
THEN 'Sin Registro'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 50
THEN '> 51'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 40 AND datediff(now(), users.UserBirthDate) / 365.25 < 51
THEN '41 - 50'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 30 AND datediff(now(), users.UserBirthDate) / 365.25 < 41
THEN '31 - 40'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 26 AND datediff(now(), users.UserBirthDate) / 365.25 < 31
THEN '27 - 30'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 20 AND datediff(now(), users.UserBirthDate) / 365.25 < 27
THEN '21 - 26'
WHEN datediff(now(), users.UserBirthDate) / 365.25 <= 20
THEN '< 21' END AS AgeGroup
FROM users
GROUP BY AgeGroup
我得到这个结果:
<table><tbody><tr><th>COUNT(users.userid)</th><th>AgeGroup</th></tr><tr><td>3</td><td>21 - 26</td></tr><tr><td>3</td><td>27 - 30</td></tr><tr><td>1</td><td>31 - 40</td></tr><tr><td>1</td><td>41 - 50</td></tr><tr><td>3</td><td>< 21</td></tr><tr><td>1</td><td>> 51</td></tr><tr><td>7</td><td>Sin Registro</td></tr></tbody></table>
但是当我运行:
SELECT COUNT(users.UserID)
FROM users
WHERE datediff(now(), users.UserBirthDate) / 365.25 > 20 AND datediff(now(), users.UserBirthDate) / 365.25 < 27;
我明白了:
<table><tbody><tr><th>COUNT(users.userid)</th></tr><tr><td>5</td></tr></tbody></table>
更新
手动检查结果后,我发现两者都不正确。
将 FLOOR()
添加到 datediff
结果,最终给出了正确的结果。
最终查询:
SELECT COUNT(users.UserID), case when users.UserBirthDate IS NULL then 'Sin Registro'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 50 then '> 51'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 40 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 51 then '41 - 50'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 30 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 41 then '31 - 40'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 26 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 31 then '27 - 30'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 20 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 27 then '21 - 26'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) <= 20 then '< 21' end as AgeGroup FROM users GROUP BY AgeGroup;
正确结果:
<table><tbody><tr><th>COUNT(users.userid)</th><th>AgeGroup</th></tr><tr><td>4</td><td>21 - 26</td></tr><tr><td>1</td><td>27 - 30</td></tr><tr><td>2</td><td>31 - 40</td></tr><tr><td>4</td><td>< 21</td></tr><tr><td>1</td><td>> 51</td></tr><tr><td>7</td><td>Sin Registro</td></tr></tbody></table>
用where + floor检查,我也得到了正确的结果。
SELECT COUNT(users.UserID)
FROM users
WHERE FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 20 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 27;
Returns4个,喜欢的话
我想由于大小写顺序的原因,大小写与 where 不同。
我在一个案例和一个地方使用 datediff 时得到不同的结果。 这是 table 用户,省略了不相关的列,所有用户:
<table><tbody><tr><th>UserID</th><th>UserTypeID</th><th>UserRegistrationDate</th><th>UserBirthDate</th><th>UserJobID</th><th>UserLocationID</th></tr><tr><td>1</td><td>1</td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>2</td><td>1</td><td>3/12/2015</td><td>30/1/1982</td><td>2</td><td>3</td></tr><tr><td>3</td><td>1</td><td>2/12/2015</td><td>19/9/1987</td><td>2</td><td>4</td></tr><tr><td>12</td><td>1</td><td>2/12/2015</td><td>11/9/1992</td><td>2</td><td>5</td></tr><tr><td>13</td><td>1</td><td>13/12/2015</td><td>15/5/1995</td><td>2</td><td>2</td></tr><tr><td>40</td><td>1</td><td>10/1/2016</td><td>22/7/1975</td><td>2</td><td>2</td></tr><tr><td>47</td><td>1</td><td>16/1/2016</td><td>29/2/2000</td><td>1</td><td>6</td></tr><tr><td>50</td><td> </td><td> </td><td>1/12/1956</td><td>2</td><td>2</td></tr><tr><td>52</td><td> </td><td> </td><td>12/3/1998</td><td>2</td><td>6</td></tr><tr><td>54</td><td>1</td><td>20/1/2016</td><td>11/4/1990</td><td>2</td><td>2</td></tr><tr><td>55</td><td>1</td><td>25/1/2016</td><td> </td><td> </td><td> </td></tr><tr><td>57</td><td> </td><td> </td><td>1/1/1990</td><td> </td><td> </td></tr><tr><td>58</td><td>1</td><td> </td><td> </td><td> </td><td> </td></tr><tr><td>59</td><td>1</td><td>28/1/2016</td><td> </td><td> </td><td> </td></tr><tr><td>60</td><td>1</td><td>13/1/2016</td><td>1/1/1998</td><td>1</td><td>10</td></tr><tr><td>61</td><td>1</td><td>1/12/2015</td><td>22/6/1989</td><td>1</td><td>12</td></tr><tr><td>66</td><td>1</td><td>1/2/2016</td><td> </td><td> </td><td> </td></tr><tr><td>67</td><td>1</td><td>1/2/2016</td><td> </td><td> </td><td> </td></tr><tr><td>68</td><td>1</td><td>1/2/2016</td><td> </td><td> </td><td> </td></tr></tbody></table>
当运行宁此:
SELECT
COUNT(users.UserID),
CASE WHEN users.UserBirthDate IS NULL
THEN 'Sin Registro'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 50
THEN '> 51'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 40 AND datediff(now(), users.UserBirthDate) / 365.25 < 51
THEN '41 - 50'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 30 AND datediff(now(), users.UserBirthDate) / 365.25 < 41
THEN '31 - 40'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 26 AND datediff(now(), users.UserBirthDate) / 365.25 < 31
THEN '27 - 30'
WHEN datediff(now(), users.UserBirthDate) / 365.25 > 20 AND datediff(now(), users.UserBirthDate) / 365.25 < 27
THEN '21 - 26'
WHEN datediff(now(), users.UserBirthDate) / 365.25 <= 20
THEN '< 21' END AS AgeGroup
FROM users
GROUP BY AgeGroup
我得到这个结果:
<table><tbody><tr><th>COUNT(users.userid)</th><th>AgeGroup</th></tr><tr><td>3</td><td>21 - 26</td></tr><tr><td>3</td><td>27 - 30</td></tr><tr><td>1</td><td>31 - 40</td></tr><tr><td>1</td><td>41 - 50</td></tr><tr><td>3</td><td>< 21</td></tr><tr><td>1</td><td>> 51</td></tr><tr><td>7</td><td>Sin Registro</td></tr></tbody></table>
但是当我运行:
SELECT COUNT(users.UserID)
FROM users
WHERE datediff(now(), users.UserBirthDate) / 365.25 > 20 AND datediff(now(), users.UserBirthDate) / 365.25 < 27;
我明白了:
<table><tbody><tr><th>COUNT(users.userid)</th></tr><tr><td>5</td></tr></tbody></table>
更新
手动检查结果后,我发现两者都不正确。
将 FLOOR()
添加到 datediff
结果,最终给出了正确的结果。
最终查询:
SELECT COUNT(users.UserID), case when users.UserBirthDate IS NULL then 'Sin Registro'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 50 then '> 51'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 40 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 51 then '41 - 50'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 30 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 41 then '31 - 40'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 26 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 31 then '27 - 30'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 20 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 27 then '21 - 26'
when FLOOR(datediff(now(), users.UserBirthDate) / 365.25) <= 20 then '< 21' end as AgeGroup FROM users GROUP BY AgeGroup;
正确结果:
<table><tbody><tr><th>COUNT(users.userid)</th><th>AgeGroup</th></tr><tr><td>4</td><td>21 - 26</td></tr><tr><td>1</td><td>27 - 30</td></tr><tr><td>2</td><td>31 - 40</td></tr><tr><td>4</td><td>< 21</td></tr><tr><td>1</td><td>> 51</td></tr><tr><td>7</td><td>Sin Registro</td></tr></tbody></table>
用where + floor检查,我也得到了正确的结果。
SELECT COUNT(users.UserID)
FROM users
WHERE FLOOR(datediff(now(), users.UserBirthDate) / 365.25) > 20 AND FLOOR(datediff(now(), users.UserBirthDate) / 365.25) < 27;
Returns4个,喜欢的话
我想由于大小写顺序的原因,大小写与 where 不同。