I have this schema with data:
CREATE TABLE `ticket` (
`id` INT NOT NULL,
`name` VARCHAR(45) NULL,
`type` INT(11) NULL,
PRIMARY KEY (`id`));
CREATE TABLE `task` (
`id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`ticket_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_task_1_idx` (`ticket_id`),
CONSTRAINT `fk_task_1` FOREIGN KEY (`ticket_id`) REFERENCES `ticket` (`id`)
) ENGINE=InnoDB;
CREATE TABLE `planing` (
`id` int(11) NOT NULL,
`date` datetime DEFAULT NULL,
`task_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_planing_1_idx` (`task_id`),
CONSTRAINT `fk_planing_1` FOREIGN KEY (`task_id`) REFERENCES `task` (`id`)
) ENGINE=InnoDB ;
INSERT INTO `ticket` (`id`, `name`, `type`) VALUES (1, 'ticket_1', 1);
INSERT INTO `task` (`id`, `name`, `ticket_id`) VALUES (1, 'task_1', 1);
INSERT INTO `task` (`id`, `name`, `ticket_id`) VALUES (2, 'task_2', 1);
INSERT INTO `planing` (`id`, `date`, `task_id`) VALUES (1, '2016-06-01 05:00', 2);
INSERT INTO `ticket` (`id`, `name`, `type`) VALUES (2, 'ticket_2', 1);
INSERT INTO `task` (`id`, `name`, `ticket_id`) VALUES (3, 'task_3', 2);
INSERT INTO `task` (`id`, `name`, `ticket_id`) VALUES (4, 'task_4', 2);
INSERT INTO `planing` (`id`, `date`, `task_id`) VALUES (2, '2016-06-01 05:00', 3);
INSERT INTO `planing` (`id`, `date`, `task_id`) VALUES (3, '2016-07-01 05:00', 4);
INSERT INTO `ticket` (`id`, `name`, `type`) VALUES (3, 'ticket_3', 1);
When I perform a select:
SELECT ticket.id AS ticket_id, task.id AS task_id, planing.date as due_date
FROM ticket
LEFT JOIN task ON task.ticket_id = ticket.id
LEFT JOIN planing ON (task.id = planing.task_id AND planing.date IS NOT NULL)
WHERE ticket.type = 1
GROUP BY ticket.id
ORDER BY planing.date ASC
;
on MySQL 5.5 I get
ticket_id | task_id | due_date
1 |1 |null
3 |null |null
2 |3 |2016-06-01 05:00:00
and on MySQL 5.6 I get
ticket_id | task_id | due_date
3 |null |null
1 |2 |2016-06-01 05:00:00
2 |3 |2016-06-01 05:00:00
How to get the same behavior on MySQL 5.5 as on MySQL 5.6 (app server uses the MySQL 5.5 version)?
id
is a recipe for confusion when debugging - maybe table_name_id? Also,date
(an SQL keyword) andtype
(also used in pgsql) - not a great idea. Just because it's possible, doesn't mean that you should do it. You should consider PostgreSQL if you want consistency in your queries.