两张表A,B 
A表里的主键id是B表的外键,举个例子
A
id   name   age 
1    allan  25
2    tom    18
3    mack   34B
id   address
1     江苏
1     上海
2     南京
3     苏州现在我搜索两张表,网页结果显示为
id   name  age  address
1    allan  25   江苏
1    allan  25   上海
2     tom   18   南京 
3     mack  34   苏州而我想要的结果为
id   name  age  address
1    allan  25   江苏 上海
2     tom   18   南京 
3     mack  34   苏州我的sql文
select A.id, A.name, A.age, B.address from A, B where A.id = B.id
现在需要使用 distinct
请问sql文如何修改?我是用php 写的
如sql文无法修改,那下面应该对搜索结果如何处理。谢谢

解决方案 »

  1.   

    你的要求和这个差不多,修改一下就可以使用了
    SQL> select * from test;ID TME---- ----------A 1B 1C 1D 1E 1F 2G 2已选择7行。SQL> select tme, substr(max(sys_connect_by_path(id, ',')), 2) catstr2 from (select id, tme, row_number() over(partition by tme order by 1) rn3 from test)4 start with rn = 15 connect by rn - 1 = prior rn and tme = prior tme6 group by tme;TME CATSTR---------- --------------------1 A,B,C,D,E2 F,GSQL>
      

  2.   

    --测试数据
    create table a(id int,name varchar2(100),age int)
    insert into a 
    select 1,'allan',25 from dual union all 
    select 2,'tom',18 from dual union all
    select 3,'mack',34 from dual;create table B(id int,ddress varchar2(100))
    insert into b 
    select 1,'江苏' from dual union all 
    select 1,'上海' from dual union all
    select 2,'南京' from dual union all
    select 3,'苏州' from dual;--执行查询
    select t.*,t1.catstr from a t,
    (select id, substr(max(sys_connect_by_path(ddress, ',')), 2) catstr
    from (select ddress, id, row_number() over(partition by id order by 1) rn
    from b)
    start with rn = 1
    connect by rn - 1 = prior rn and id = prior id
    group by id) t1
    where t.id=t1.id
    --输出结果
    1 allan 25 江苏,上海
    2 tom 18 南京
    3 mack 34 苏州