I have to create a column that shows the duration between the date of a booking to a hotel and either the date of cancellation or the date of entrance.
I tried many things, but I'm getting errors constantly. I believe that it has to do with the datatype of the resulting column (I tried INT, TIME, DATE and TIMESTAMP).
ALTER TABLE reservations.hotel_res
ADD duration_res TIME;
INSERT INTO reservations.hotel_res (duration_res)
SELECT CASE WHEN date_cancellation IS NULL THEN TIMESTAMPDIFF(day,date_booking,date_entrance)
ELSE TIMESTAMPDIFF(day,date_booking,date_cancellation)
END FROM reservations.hotel_res;
You can add a virtual column to the table (details: see the documentation).
Example: DDL code (MySQL 5.7)
INSERTs
Query
If you just want to add the virtual column without re-creating the table, use ...
DBfiddle here.