SQl查询语句写法

来源:百度知道 编辑:UC知道 时间:2024/05/20 02:02:45
A表: type_id 和 type_name 两个字段
B表:type_id 和 ype_tel 两个字段
A表 有一条数据: 1 ,成XX
B表 有两条数据: 1 , 010
1, 020
查询出来的数据有两条1:成XX,010,
2:成XX,020,

因为type_id 是一样的所以我想要查询出来的结果是一条记录例如:
成XX,010,020

请问大侠们这句SQL怎么写?
如果A表在加一条记录: 2,信息XX

B表相应的加3条记录:1( 2 ,0111)
2(2,0222)
3(2.0333)

出来的结果就应该是
第一条:成XX,010,020 ,null
第二条:信息XX,0111,0222,0333

--修改-------------------
字段名别用type_name 貌似是个函数~~~ 在SQL2000测试过~~~~ 8个应该应该够用了吧?不够的话继续加就OK了。。 还真不好写
select A.type_name,W.XX from A,(select type_id,max(case when num=1 then type_tel+',' else null end)+
max(case when num=2 then type_tel+',' else null end)+
max(case when num=3 then type_tel+',' else null end)+
max(case when num=4 then type_tel+',' else null end)+
max(case when num=5 then type_tel+',' else null end)+
max(case when num=6 then type_tel+',' else null end)+
max(case when num=7 then type_tel+',' else null end)+
max(case when num=8 then type_tel+',' else null end) as XX from (select (select count(*) from B where type_tel<=K.type_tel) as num,* from B K) D
group by type_id) W

select * from B where type_id in (select * from A where type_id=1)