Q. Create a new column DaysTakenForDelivery that contains the date difference between Order_Date and Ship_Date.
Tables available are: orders and shipping
CREATE TABLE orders (
Order_ID int DEFAULT NULL,
Order_Date text,
Order_Priority text,
Ord_id text
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
CREATE TABLE shipping (
Order_ID int DEFAULT NULL,
Ship_Mode text,
Ship_Date text,
Ship_id text,
DaysTakenForDelivery` int DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Row counts:
- shipping: 7701
- orders: 5506
Please note I changed the datatyes for date columns properly.
Things I did:
I added the required column in the table 'shipping', since nothing was specified with respect to that (which table it should be added to or otherwise). Query for the same:
ALTER TABLE shipping ADD DaysTakenForDelivery INT;
Next, I tried to update the column using various queries but nothing worked. A few of them are listed below:
FAILED ATTEMPTS:
UPDATE shipping SET DaysTakenForDelivery = (
select datediff(b.ship_date, a.order_date) AS DaysTakenForDelivery
from orders a
JOIN shipping b ON a.Order_ID = b.Order_ID
);
NOTE: this query led to the following error:
Error Code: 1093. You can't specify target table 'shipping_dimen' for update in FROM clause
Next query I tried:
UPDATE shipping b SET DaysTakenForDelivery = (
select datediff(b.ship_date, a.order_date) AS DaysTakenForDelivery
from orders a
WHERE a.Order_ID = b.Order_ID
);
NOTE: this query led to the following error:
Error Code: 1242. Subquery returns more than 1 row
How am I supposed to achieve the desired result?
Please note I am using MySQL and answers for the same RDBMS would be appreciated for better understanding.
Version I am using: 8.0.31