gpt4 book ai didi

mysql - 计算 datediff MySQL 时的差异

转载 作者:行者123 更新时间:2023-11-29 02:51:24 26 4
gpt4 key购买 nike

在一个案例和一个地方使用 datediff 时,我得到了不同的结果。这是用户表,省略了不相关的列,并且包含所有用户:

<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>&lt; 21</td></tr><tr><td>1</td><td>&gt; 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>

更新

SQLFiddle with complete tables and data.

最佳答案

在手动检查结果后,我意识到两者都不正确。将 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>&lt; 21</td></tr><tr><td>1</td><td>&gt; 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;

像这种情况一样返回 4。

我想由于大小写顺序的原因,大小写与 where 不同。

关于mysql - 计算 datediff MySQL 时的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35248263/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com