我看到新的mysql可以做递归查询!雅虎!有人能帮我翻译一下,这样我就可以看到它在mysql v8上工作了吗?
WITH gov AS (
SELECT 1 AS level, * FROM gov_unit where parent_id is null
UNION ALL
SELECT gov.level+1 AS level, gov_unit.* FROM gov_unit
INNER JOIN gov ON (gov_unit.parent_id = gov.id)
) SELECT * FROM gov;
显然,“select1aslevel”的mysql语法有点不同。
下面是我最喜欢的书“pocket guide to sql”中的示例表:
create table gov_unit (
id integer NOT NULL,
parent_id integer DEFAULT 3,
name varchar(10),
type varchar(8),
constraint gov_unit_pk
primary key (id),
constraint gov_unit_type_chk
check (type in ('County','Township','City','State')),
constraint gov_unit_loop
foreign key (parent_id)
references gov_unit(id)
) engine=innodb;
以下是一些示例数据:
insert into gov_unit values (3,null,'Michigan','State');
insert into gov_unit values (2,3,'Alger','County');
insert into gov_unit values (1,2,'Munising','City');
insert into gov_unit values (4,2,'Munising','Township');
insert into gov_unit values (5,2,'Au Train','Township');
insert into gov_unit values (6,3,'Baraga','County');
insert into gov_unit values (7,3,'Ontonagon','County');
insert into gov_unit values (8,7,'Interior','Township');
insert into gov_unit values (9,3,'Dickinson','County');
insert into gov_unit values (10,3,'Gogebic','County');
insert into gov_unit values (11,3,'Delta','County');
insert into gov_unit values (12,11,'Masonville','Township');
1条答案
按热度按时间dfddblmv1#
有两个问题:(1)我需要说“政府单位”而不是“谢谢你”(2) 我需要将“with gov”改为“with recursive gov”。现在成功了!