v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with recursive ancestors as (select `folks`.`id` AS `id`,`folks`.`name` AS `name`,`folks`.`dob` AS `dob`,`folks`.`father` AS `father`,`folks`.`mother` AS `mother` from `folks` where ((`folks`.`name` = 'Vasya') and (`folks`.`dob` = '2000-01-01')) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `a`) where ((`p`.`id` = `a`.`father`) or (`p`.`id` = `a`.`mother`)))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors` latin1 latin1_swedish_ci
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS with recursive ancestors as (select `folks`.`id` AS `id`,`folks`.`name` AS `name`,`folks`.`dob` AS `dob`,`folks`.`father` AS `father`,`folks`.`mother` AS `mother` from `folks` where ((`folks`.`name` = 'Me') and (`folks`.`dob` = '2000-01-01')) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `a`) where ((`p`.`id` = `a`.`father`) or (`p`.`id` = `a`.`mother`)))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors` latin1 latin1_swedish_ci
v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with recursive ancestors as (select `folks`.`id` AS `id`,`folks`.`name` AS `name`,`folks`.`dob` AS `dob`,`folks`.`father` AS `father`,`folks`.`mother` AS `mother` from `folks` where (`folks`.`name` = 'Vasya') union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `fa`) where (`p`.`id` = `fa`.`father`) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `ma`) where (`p`.`id` = `ma`.`mother`))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors` latin1 latin1_swedish_ci
v2 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v2` AS with recursive ancestors as (select `folks`.`id` AS `id`,`folks`.`name` AS `name`,`folks`.`dob` AS `dob`,`folks`.`father` AS `father`,`folks`.`mother` AS `mother` from `folks` where (`folks`.`name` = 'Me') union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `fa`) where (`p`.`id` = `fa`.`father`) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from (`folks` `p` join `ancestors` `ma`) where (`p`.`id` = `ma`.`mother`))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors` latin1 latin1_swedish_ci
select * from v2;
id name dob father mother
100 Vasya 2000-01-01 20 30
100 Me 2000-01-01 20 30
20 Dad 1970-02-02 10 9
30 Mom 1975-03-03 8 7
9 Grandma Ann 1941-10-15 NULL NULL
...
...
@@ -398,7 +398,7 @@ as
(
select *
from folks
where name = 'Vasya' and dob = '2000-01-01'
where name = 'Me' and dob = '2000-01-01'
union
select p.id, p.name, p.dob, p.father, p.mother
from folks as p, ancestors AS a
...
...
@@ -412,14 +412,14 @@ id select_type table type possible_keys key key_len ref rows filtered Extra
3 UNCACHEABLE UNION <derived2> ALL NULL NULL NULL NULL 12 100.00 Using where; Using join buffer (flat, BNL join)
NULL UNION RESULT <union2,3> ALL NULL NULL NULL NULL NULL NULL
Warnings:
Note 1003 with recursive ancestors as (select `test`.`folks`.`id` AS `id`,`test`.`folks`.`name` AS `name`,`test`.`folks`.`dob` AS `dob`,`test`.`folks`.`father` AS `father`,`test`.`folks`.`mother` AS `mother` from `test`.`folks` where ((`test`.`folks`.`name` = 'Vasya') and (`test`.`folks`.`dob` = DATE'2000-01-01')) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from `test`.`folks` `p` join `ancestors` `a` where ((`a`.`father` = `p`.`id`) or (`a`.`mother` = `p`.`id`)))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors`
Note 1003 with recursive ancestors as (select `test`.`folks`.`id` AS `id`,`test`.`folks`.`name` AS `name`,`test`.`folks`.`dob` AS `dob`,`test`.`folks`.`father` AS `father`,`test`.`folks`.`mother` AS `mother` from `test`.`folks` where ((`test`.`folks`.`name` = 'Me') and (`test`.`folks`.`dob` = DATE'2000-01-01')) union select `p`.`id` AS `id`,`p`.`name` AS `name`,`p`.`dob` AS `dob`,`p`.`father` AS `father`,`p`.`mother` AS `mother` from `test`.`folks` `p` join `ancestors` `a` where ((`a`.`father` = `p`.`id`) or (`a`.`mother` = `p`.`id`)))select `ancestors`.`id` AS `id`,`ancestors`.`name` AS `name`,`ancestors`.`dob` AS `dob`,`ancestors`.`father` AS `father`,`ancestors`.`mother` AS `mother` from `ancestors`
with recursive
ancestor_ids (id)
as
(
select father from folks where name = 'Vasya'
select father from folks where name = 'Me'
union
select mother from folks where name = 'Vasya'
select mother from folks where name = 'Me'
union
select father from folks, ancestor_ids a where folks.id = a.id
union
...
...
@@ -446,7 +446,7 @@ as
(
select *
from folks
where name = 'Vasya'
where name = 'Me'
union all
select p.*
from folks as p, ancestors as fa
...
...
@@ -458,7 +458,7 @@ where p.id = ma.mother
)
select * from ancestors;
id name dob father mother
100 Vasya 2000-01-01 20 30
100 Me 2000-01-01 20 30
20 Dad 1970-02-02 10 9
30 Mom 1975-03-03 8 7
9 Grandma Ann 1941-10-15 NULL NULL
...
...
@@ -471,9 +471,9 @@ with recursive
ancestor_ids (id, generation)
as
(
select father, 1 from folks where name = 'Vasya' and father is not null
select father, 1 from folks where name = 'Me' and father is not null
union all
select mother, 1 from folks where name = 'Vasya' and mother is not null
select mother, 1 from folks where name = 'Me' and mother is not null
union all
select father, fa.generation+1 from folks, ancestor_ids fa
where folks.id = fa.id and (father not in (select id from ancestor_ids))
...
...
@@ -490,9 +490,9 @@ with recursive
ancestor_ids (id, generation)
as
(
select father from folks where name = 'Vasya' and father is not null
select father from folks where name = 'Me' and father is not null
union all
select mother from folks where name = 'Vasya' and mother is not null
select mother from folks where name = 'Me' and mother is not null
union all
select father, fa.generation+1 from folks, ancestor_ids fa
where folks.id = fa.id and (father not in (select id from ancestor_ids))
...
...
@@ -507,9 +507,9 @@ with recursive
ancestor_ids (id, generation)
as
(
select father, 1 from folks where name = 'Vasya' and father is not null
select father, 1 from folks where name = 'Me' and father is not null
union all
select mother, 1 from folks where name = 'Vasya' and mother is not null
select mother, 1 from folks where name = 'Me' and mother is not null
union all
select father, fa.generation+1 from folks, ancestor_ids fa
where folks.id = fa.id and father is not null and
...
...
@@ -535,9 +535,9 @@ with recursive
coupled_ancestor_ids (id)
as
(
select father from folks where name = 'Vasya' and father is not null
select father from folks where name = 'Me' and father is not null
union
select mother from folks where name = 'Vasya' and mother is not null
select mother from folks where name = 'Me' and mother is not null
union
select n.father
from folks, coupled_ancestor_ids fa, coupled_ancestor_ids ma, folks n
...
...
@@ -559,9 +559,9 @@ with recursive
coupled_ancestor_ids (id)
as
(
select father from folks where name = 'Vasya' and father is not null
select father from folks where name = 'Me' and father is not null
union
select mother from folks where name = 'Vasya' and mother is not null
select mother from folks where name = 'Me' and mother is not null
union
select n.father
from folks, coupled_ancestor_ids fa, coupled_ancestor_ids ma, folks n
...
...
@@ -588,9 +588,9 @@ with recursive
ancestor_ids (id)
as
(
select father from folks where name = 'Vasya'
select father from folks where name = 'Me'
union
select mother from folks where name = 'Vasya'
select mother from folks where name = 'Me'
union
select father from folks left join ancestor_ids a on folks.id = a.id
union
...
...
@@ -609,9 +609,9 @@ with recursive
ancestor_ids (id)
as
(
select father from folks where name = 'Vasya'
select father from folks where name = 'Me'
union
select mother from folks where name = 'Vasya'
select mother from folks where name = 'Me'
union
select father from folks left join ancestor_ids a on folks.id = a.id
union
...
...
@@ -638,9 +638,9 @@ with recursive
ancestor_ids (id, generation)
as
(
select father, 1 from folks where name = 'Vasya'
select father, 1 from folks where name = 'Me'
union
select mother, 1 from folks where name = 'Vasya'
select mother, 1 from folks where name = 'Me'
union
select father, a.generation+1 from folks, ancestor_ids a
where folks.id = a.id
...
...
@@ -667,9 +667,9 @@ with recursive
ancestor_ids (id, generation)
as
(
select father, 1 from folks where name = 'Vasya'
select father, 1 from folks where name = 'Me'
union
select mother, 1 from folks where name = 'Vasya'
select mother, 1 from folks where name = 'Me'
union
select max(father), max(a.generation)+1 from folks, ancestor_ids a
where folks.id = a.id
...
...
@@ -692,9 +692,9 @@ with recursive
ancestor_ids (id, generation)
as
(
select father, 1 from folks where name = 'Vasya'
select father, 1 from folks where name = 'Me'
union
select mother, 1 from folks where name = 'Vasya'
select mother, 1 from folks where name = 'Me'
union
select max(father), a.generation+1 from folks, ancestor_ids a