把 某个人的id 以及 它父亲pid 的给你
要你 给出一条查询语句 能把 当过爷爷的人 的姓名 给出来答案 给出 5和8
create table pid
(
id int primary key auto_increment,
pid int,
name varchar(25)
);
insert into pid values (1, 4,'1AAA');
insert into pid values (2, 4,'2BBB');
insert into pid values (3, 5,'3CCC');
insert into pid values (4, 5,'4DDD');
insert into pid values (5, 8,'5AAA');
insert into pid values (6, 3,'6AAA');
insert into pid values (7, 3,'7AAA');
insert into pid values (8, null,'8AAA');
insert into pid values (9,8 ,'9AAA');我当时只想到过 自连接
这样只能找到 父亲的名字 和 儿子的名字
select p1.name,p2.name from pid p1,pid p2 where p1.id=p2.pid;
还请 不吝赐教
要你 给出一条查询语句 能把 当过爷爷的人 的姓名 给出来答案 给出 5和8
create table pid
(
id int primary key auto_increment,
pid int,
name varchar(25)
);
insert into pid values (1, 4,'1AAA');
insert into pid values (2, 4,'2BBB');
insert into pid values (3, 5,'3CCC');
insert into pid values (4, 5,'4DDD');
insert into pid values (5, 8,'5AAA');
insert into pid values (6, 3,'6AAA');
insert into pid values (7, 3,'7AAA');
insert into pid values (8, null,'8AAA');
insert into pid values (9,8 ,'9AAA');我当时只想到过 自连接
这样只能找到 父亲的名字 和 儿子的名字
select p1.name,p2.name from pid p1,pid p2 where p1.id=p2.pid;
还请 不吝赐教
from pid p1,pid p2,pid p2
where p1.id=p2.pid
and p2.id=p3.pid;
select p1.name,p2.name,p3.name
from pid p1,pid p2,pid p3
where p1.id=p2.pid
and p2.id=p3.pid;
目前 另一种方案:select * from pid p where p.id in (select c.pid from pid c where c.id in
( select cc.pid from pid cc ));
select * from pid p where p.id in (select c.pid from pid c where c.id in
( select cc.pid from pid cc ));嗯,这个好,没有重复。
from pid p1,pid p2,pid p3
where p1.id=p2.pid
and p2.id=p3.pid