Skip to content
Advertisement

Update columns based on calculation

My table looks like this:

I have a list of id’s connected to datestamps. I can manage to calculate the difference between their latest and first entry as follows:

However, I am unsure how I can update my table to reflect these calculations. What I want is the following:

Advertisement

Answer

In MySQL, you can self-join:

I would not necessarily recommend storing this derived information, because it is hard to keep it up to date. Instead, you can create a view. If you are running MySQL 8.0:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement