Home > Enterprise >  Difference in result ( MySQL 5.7 vs MySQL 8.0 )
Difference in result ( MySQL 5.7 vs MySQL 8.0 )

Time:02-22

I'm wondering why this query shows different results in different versions of MySQL:

https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=409db6dd827acd7bda4ff9723fa108d9

In the MySQL 8 fiddle, the slot which lasts from 10:00 to 10:30 should not be shown because there is an appointment from 10:00 to 10:30 in the table.

MySQL 5.7:

 
CREATE TABLE shifts (
    id int NOT NULL,
    unique_id varchar(255) NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE appointments (
    id int NOT NULL,
    unique_id varchar(255) NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    shift_id int NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (shift_id) REFERENCES shifts(id)
);

INSERT INTO `shifts` (`id`, `unique_id`, `start_date`, `end_date`)
VALUES
    ('4596', '614fc0c5dab2485bba8cbbd9eb98aa53', '2022-02-28 10:00:00', '2022-03-15 19:00:00');


INSERT INTO `appointments` (`id`, `unique_id`, `start_date`, `end_date`, `shift_id`)
VALUES
    (18382, '1371e41a7cf342bfa279a8b120cc5f43', '2022-02-28 10:00:00', '2022-02-28 10:30:00', 4596),
    (18517, '124baaa1aeab421cb288f2b7c7abe89b', '2022-02-28 10:30:00', '2022-02-28 11:00:00', 4596),
    (18523, 'aff5abe6e8f84a64ace3277d3cd6dbd1', '2022-02-28 11:00:00', '2022-02-28 12:00:00', 4596),
    (18355, 'ec570f9365bb4ad68c7b5ab3b7d9aeea', '2022-02-28 12:00:00', '2022-02-28 12:30:00', 4596),
    (18537, 'e1946add03cd412da357b58c93d58c6e', '2022-02-28 12:30:00', '2022-02-28 13:15:00', 4596),
    (18540, 'e16af8a053594e8b8340ede302398a22', '2022-02-28 13:30:00', '2022-02-28 14:00:00', 4596),
    (18235, 'deac969701ae47f0a78696f12c267475', '2022-02-28 14:00:00', '2022-02-28 14:30:00', 4596),
    (18462, '3270929ae6e546fb824e6d3219917b8e', '2022-02-28 14:30:00', '2022-02-28 16:00:00', 4596),
    (18544, 'd27bc7ba34a74f078966ebe1567d3181', '2022-02-28 16:00:00', '2022-02-28 16:30:00', 4596),
    (18622, '02ca48b35ca3462f9030bcb97ea1dbab', '2022-02-28 16:30:00', '2022-02-28 16:50:00', 4596),
    (18545, '4a49c054e59e4514ae2a521b55a7715c', '2022-02-28 16:50:00', '2022-02-28 17:15:00', 4596),
    (18351, '6a0d56ad43894b60b4f8a289fbbfff73', '2022-02-28 17:30:00', '2022-02-28 18:15:00', 4596),
    (18352, 'accdc12943954ee2bd69ad116c2fef3d', '2022-02-28 18:15:00', '2022-02-28 19:00:00', 4596);


SELECT Available_from, Available_to
  FROM (
    SELECT COALESCE(@lasttime_to, '2022-02-28 10:00:00') AS Available_from, start_date AS Available_to, @lasttime_to := end_date
    FROM (SELECT start_date, end_date
             FROM appointments
             WHERE shift_id = 4596
             AND end_date <= '2022-02-28 19:00:00'
             AND start_date >= '2022-02-28 10:00:00'
             UNION ALL (
               SELECT '2022-02-28 19:00:00', '2022-02-28 19:00:00'
             )
             UNION ALL (
               SELECT '2022-02-28 10:00:00', '2022-02-28 10:00:00'
             )
               ORDER BY start_date 
           ) e
    JOIN (SELECT @lasttime_to := NULL) init) x
    WHERE Available_to > DATE_ADD(Available_from, INTERVAL 14 MINUTE);


Results:

Available_from  Available_to
2022-02-28 13:15:00 2022-02-28 13:30:00
2022-02-28 17:30:00 2022-02-28 17:30:00

MySQL 8.0:

 
CREATE TABLE shifts (
    id int NOT NULL,
    unique_id varchar(255) NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    PRIMARY KEY (id)
);

CREATE TABLE appointments (
    id int NOT NULL,
    unique_id varchar(255) NOT NULL,
    start_date DATETIME NOT NULL,
    end_date DATETIME NOT NULL,
    shift_id int NOT NULL,
    PRIMARY KEY (id),
    FOREIGN KEY (shift_id) REFERENCES shifts(id)
);

INSERT INTO `shifts` (`id`, `unique_id`, `start_date`, `end_date`)
VALUES
    ('4596', '614fc0c5dab2485bba8cbbd9eb98aa53', '2022-02-28 10:00:00', '2022-03-15 19:00:00');


INSERT INTO `appointments` (`id`, `unique_id`, `start_date`, `end_date`, `shift_id`)
VALUES
    (18382, '1371e41a7cf342bfa279a8b120cc5f43', '2022-02-28 10:00:00', '2022-02-28 10:30:00', 4596),
    (18517, '124baaa1aeab421cb288f2b7c7abe89b', '2022-02-28 10:30:00', '2022-02-28 11:00:00', 4596),
    (18523, 'aff5abe6e8f84a64ace3277d3cd6dbd1', '2022-02-28 11:00:00', '2022-02-28 12:00:00', 4596),
    (18355, 'ec570f9365bb4ad68c7b5ab3b7d9aeea', '2022-02-28 12:00:00', '2022-02-28 12:30:00', 4596),
    (18537, 'e1946add03cd412da357b58c93d58c6e', '2022-02-28 12:30:00', '2022-02-28 13:15:00', 4596),
    (18540, 'e16af8a053594e8b8340ede302398a22', '2022-02-28 13:30:00', '2022-02-28 14:00:00', 4596),
    (18235, 'deac969701ae47f0a78696f12c267475', '2022-02-28 14:00:00', '2022-02-28 14:30:00', 4596),
    (18462, '3270929ae6e546fb824e6d3219917b8e', '2022-02-28 14:30:00', '2022-02-28 16:00:00', 4596),
    (18544, 'd27bc7ba34a74f078966ebe1567d3181', '2022-02-28 16:00:00', '2022-02-28 16:30:00', 4596),
    (18622, '02ca48b35ca3462f9030bcb97ea1dbab', '2022-02-28 16:30:00', '2022-02-28 16:50:00', 4596),
    (18545, '4a49c054e59e4514ae2a521b55a7715c', '2022-02-28 16:50:00', '2022-02-28 17:15:00', 4596),
    (18351, '6a0d56ad43894b60b4f8a289fbbfff73', '2022-02-28 17:30:00', '2022-02-28 18:15:00', 4596),
    (18352, 'accdc12943954ee2bd69ad116c2fef3d', '2022-02-28 18:15:00', '2022-02-28 19:00:00', 4596);


SELECT Available_from, Available_to
  FROM (
    SELECT COALESCE(@lasttime_to, '2022-02-28 10:00:00') AS Available_from, start_date AS Available_to, @lasttime_to := end_date
    FROM (SELECT start_date, end_date
             FROM appointments
             WHERE shift_id = 4596
             AND end_date <= '2022-02-28 19:00:00'
             AND start_date >= '2022-02-28 10:00:00'
             UNION ALL (
               SELECT '2022-02-28 19:00:00', '2022-02-28 19:00:00'
             )
             UNION ALL (
               SELECT '2022-02-28 10:00:00', '2022-02-28 10:00:00'
             )
               ORDER BY start_date 
           ) e
    JOIN (SELECT @lasttime_to := NULL) init) x
    WHERE Available_to > DATE_ADD(Available_from, INTERVAL 14 MINUTE);


Results:


Available_from  Available_to
2022-02-28 10:00:00 2022-02-28 10:30:00
2022-02-28 13:15:00 2022-02-28 13:30:00
2022-02-28 17:15:00 2022-02-28 17:30:00

CodePudding user response:

Your subquery e is ordered by start_date, which is non-deterministic as there can be several lines with the same value.

With MySql 5.7:

  1. The first row of this subquery is the fake one you created via your union. Its from_date is 2022-02-28 10:00:00 and its end_date is 2022-02-28 10:00:00, which is going to be the Available_from of the next row because of your query usage of @lasttime_to.
  2. Next row is the one related to appointment 18382 which becomes "from 2022-02-28 10:00:00 to 2022-02-28 10:00:00", with an end_date set to 2022-02-28 10:30:00, which is going to be the Available_from of the next row.
  3. Next row is the one related to appointment 18517 which becomes "from 2022-02-28 10:30:00 to 2022-02-28 10:30:00"

So here, none of these 3 lines matches the criteria Available_to > DATE_ADD(Available_from, INTERVAL 14 MINUTE);

With MySql 5.8:

  1. The first row of this subquery is the one related to the appointment 18382 instead of the fake one. Its from_date is 2022-02-28 10:00:00 and its end_date is 2022-02-28 10:30:00, which is going to be the Available_from of the next row because of your query usage of @lasttime_to.
  2. Next row is the fake one you created via your union. It becomes "2022-02-28 10:30:00 2022-02-28 10:00:00" (!), and its end_date is 2022-02-28 10:00:00, which is going to be the Available_from of the next row.
  3. Next row is the one related to appointment 18517, which becomes "2022-02-28 10:00:00 to 2022-02-28 10:30:00".

Here, this third row matches the criteria Available_to > DATE_ADD(Available_from, INTERVAL 14 MINUTE);, which is why you get an additional row.

TL;DR: The Available_from values of your rows rely on a non-deterministic order obtained by the subquery e. Your results are then not consistent from an engine to another.

  • Related