各位大哥大姐,帮小弟写一个SQL语句啊....
表结构如下:
tableA tableB tableC
A_id A_name A_id C_id C_id C_name
1 AAA 1 1 1 C1
1 2 2 C2
1 3 3 C3
要求根据A_id 找出所有的 C_id C_name
怎么写啊
..................
------解决方案--------------------select *
from a
join b on a.a_id = b.a_id
join c on c.c_id = b.c_id
------解决方案--------------------表结构如下:
tableA tableB tableC
A_id A_name A_id C_id C_id C_name
1 AAA 1 1 1 C1
1 2 2 C2
1 3 3 C3
select c_id,c_name from a,
(
select b.*,c.* from b,c where b.c_id = c.c_id
) t
where a.a_id = t.a_id
------解决方案--------------------select a.A_id ,b.C_id,c.C_name from tableA a inner join
(select a.A_id,b.C_name
from tableB a inner join tableC b on a.A_id=b.C_id) t on a.A_id=t.A_id
------解决方案-------------------- create table tableA(A_id int,A_name varchar(10))
insert tableA
select 1, 'AAA '
create table tableB(A_id int,C_id int)
insert tableB
select 1,1
union select 1,2
union select 1,3
create table tableC(C_id int,C_name varchar(10))
insert tableC
select 1, 'C1 '
union select 2, 'C2 '
union select 3, 'C3 '
select a.a_id,b.c_id,c.c_name from tablea a left join tableb b on a.a_id=b.a_id
left join tablec c on b.c_id=c.c_id
a_id c_id c_name
----------- ----------- ----------
1 1 C1
1 2 C2
1 3 C3
(3 row(s) affected)