programing

MariaDb 쿼리를 MySQL로 변환

newsource 2022. 11. 26. 08:37

MariaDb 쿼리를 MySQL로 변환

mariaDB에 쿼리가 있습니다.이 쿼리는 (자녀(개)의 조상을 레벨 5까지 취득하는 데 사용됩니다.)

WITH RECURSIVE
cte AS (
SELECT *, 0 level, '      ' relation
FROM dogs
WHERE dog_id = 7

UNION ALL

SELECT dogs.*, level + 1, 'father'
FROM dogs
JOIN cte ON cte.father_id = dogs.dog_id
WHERE level < 5

UNION ALL

SELECT dogs.*, level + 1, 'mother'
FROM dogs
JOIN cte ON cte.mother_id = dogs.dog_id
WHERE level < 5
)
SELECT *
FROM cte
ORDER BY level, relation; 

이 쿼리를 mysql 버전으로 해주세요: 5.6.45

테이블 스크립트는 다음과 같습니다.

CREATE TABLE `dogs` (
  `dog_id` int(11) NOT NULL,
  `name` varchar(255) DEFAULT NULL,
  `father_id` int(11) DEFAULT NULL,
  `mother_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`dog_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO `dogs` VALUES ('0', null, null, null);
INSERT INTO `dogs` VALUES ('1', 'Father', null, null);
INSERT INTO `dogs` VALUES ('2', 'Mother', null, null);
INSERT INTO `dogs` VALUES ('3', 'Father1', null, null);
INSERT INTO `dogs` VALUES ('4', 'Mother2', null, null);
INSERT INTO `dogs` VALUES ('5', 'Son', '1', '2');
INSERT INTO `dogs` VALUES ('6', 'Daughter', '3', '4');
INSERT INTO `dogs` VALUES ('7', 'GrandSon', '5', '6');

언급URL : https://stackoverflow.com/questions/60224265/convert-mariadb-query-into-mysql