简体   繁体   English

在MySQL中,将每行中的值设置为相同行上的DATEDIFF计算

[英]In MySQL, set value in each row to a DATEDIFF computation on the same rows

It was very tricky to figure out what to title this question, so if anyone has any ideas for improvements feel free to edit :-). 弄清楚这个问题的标题是非常棘手的,所以如果有人有任何改进的想法,请随时编辑:-)。

Here's the deal. 这是交易。 I have a MySQL table that includes a bunch of donations, and there's a date for each donation. 我有一个MySQL表,其中包括一堆捐款,每个捐款都有一个日期。 I also have a years_active column. 我还有一个years_active专栏。 I need to run a query that will SET the years active for every row to the difference (in years) from the first date to the last date for each unique user. 我需要运行一个查询,将每个活动的年份SET为每个唯一用户从第一个日期到最后一个日期的差异(以年为单位)。

So this is my starting table: 所以这是我的起点:

------------------------------------------------------------
|  user_id    |   donation    |    date     | years_active |
------------------------------------------------------------
|     1       |     $10       | 2002-01-01  |     null     |
|     1       |     $15       | 2005-01-01  |     null     |
|     1       |     $20       | 2009-01-01  |     null     |
|     2       |     $10       | 2003-01-01  |     null     |
|     2       |     $5        | 2006-01-01  |     null     |
|     3       |     $15       | 2001-01-01  |     null     |
------------------------------------------------------------

And this is the table I'd like to achieve: 这是我想要实现的表格:

------------------------------------------------------------
|  user_id    |   donation    |    date     | years_active |
------------------------------------------------------------
|     1       |     $10       | 2002-01-01  |     8        |
|     1       |     $15       | 2005-01-01  |     8        |
|     1       |     $20       | 2009-01-01  |     8        |
|     2       |     $10       | 2003-01-01  |     4        |
|     2       |     $5        | 2006-01-01  |     4        |
|     3       |     $15       | 2001-01-01  |     1        |
------------------------------------------------------------

I know that it's far from ideal to be storing the years_active redundantly in multiple rows like this. 我知道将years_active冗余地存储在像这样的多行中是远非理想的。 Unfortunately this table is for data visualizations and with my software I have absolutely no ability to restructure the data altogether; 不幸的是,这个表用于数据可视化,而且我的软件完全无法重新构建数据; the years_active MUST be in every row. years_active必须在每一行。

In my research it seems like I would use subqueries to get the MIN value for each user id and the MAX value for each unique user id, and then do a DATEDIFF on those, and set the result to the column. 在我的研究中,似乎我会使用子查询来获取每个用户ID的MIN值和每个唯一用户ID的MAX值,然后对这些用户ID执行DATEDIFF ,并将结果设置为列。 But I don't really understand how I would run all these queries over and over again for every unique user. 但我真的不明白如何为每个独特的用户反复运行所有这些查询。

Can someone point me in the right direction? 有人能指出我正确的方向吗? Is this possible? 这可能吗?

SELECT t1.user_id, t1.donation, t1.date, t2.years_active
FROM yourTable t1
INNER JOIN
(
    SELECT user_id, MAX(YEAR(date)) - MIN(YEAR(date)) + 1 AS years_active
    FROM yourTable
    GROUP BY user_id
) t2
    ON t1.user_id = t2.user_id

Follow the link below for a running demo: 请点击以下链接查看正在运行的演示:

SQLFiddle SQLFiddle

Update: 更新:

Here is an UPDATE statement which will assign the years_active column the correct values: 这是一个UPDATE语句,它将为years_active列分配正确的值:

UPDATE yourTable t1 
INNER JOIN
(
    SELECT user_id, MAX(YEAR(date)) - MIN(YEAR(date)) + 1 AS years_active
    FROM yourTable
    GROUP BY user_id
) t2
    ON t1.user_id = t2.user_id
SET t1.years_active = t2.years_active

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM