表t_bind(id,reg_no,bind_no)
一个注册号码reg_no只能有3个绑定号码,请教一下:怎么得到这样的结果啊,谢谢
reg_no,bind_no1,bind_no2,bind_no3
02098339935,13366666666,15366666666,18966666666

解决方案 »

  1.   

    是一个表中关于某个reg_no,最多不能超过3条记录,同时,这三条记录的bind_no是不能一样的,是吗?
      

  2.   

    分成2张表来做,用的时候把2张表联合起来查就可以了..t_bind(id,reg_no,bind_no)t_reg(reg_no,bind_no)select * from t_bind,t_reg where t_bind.reg_no=t_reg.reg_no;
      

  3.   

    是的,有一个注册号码表专门存注册号码信息的,与t_bind通过外键关联
      

  4.   

    02098339935,13366666666
    02098339935,15366666666
    02098339935,18966666666
    先记录,上面的数据,然后要调用的时候,在做行转行!
    ORALCE 10G用本身的函数wm_concat
    oracle 9i 用Sys_Connect_By_Path
      

  5.   

    我是想用标准sql写来,不局限于特定数据库,有方法吗
      

  6.   

    select reg_no,max(case when rn=1 then bind_no) bind_no1,
       max(case when rn=2 then bind_no) bind_no2,
       max(case when rn=3 then bind_no) bind_no3 from(
        select reg_no,bind_no,row_number()over(partition by reg_no order by bind_no)rn
          from t_bind)
        group by reg_no  
      

  7.   

    这个不通用吧
    我现在mysql下做测试的
      

  8.   

    create table t_bind(
    id number(4),
    reg_no varchar2(20),
    bind_no varchar2(20)
    );
    insert into t_bind values(1,'02098339935','13366666666');
    insert into t_bind values(2,'02098339935','15366666666');
    insert into t_bind values(3,'02098339935','18966666666');
    insert into t_bind values(4,'02098339931','13366666666');
    insert into t_bind values(5,'02098339931','15366666666');
    insert into t_bind values(6,'02098339931','18966666666');select t1.reg_no,t1.BIND_NO,t2.bind_no,t3.bind_no from 
    (select rownum as num,reg_no,bind_no from t_bind where reg_no='02098339931') t1  
    left join  (select rownum as num,reg_no,bind_no from t_bind where reg_no='02098339931') t2 on t1.reg_no=t2.reg_no and t2.num=2
    left join  (select rownum as num,reg_no,bind_no from t_bind where reg_no='02098339931') t3 on t1.reg_no=t3.reg_no and t3.num=3 
    where t1.num=1 ; 
    查询结果:
    1 02098339931 13366666666 15366666666 18966666666
      

  9.   


    以上少了一个  endselect reg_no,max(case when rn=1 then bind_no end) bind_no1,
       max(case when rn=2 then bind_no end) bind_no2,
       max(case when rn=3 then bind_no end) bind_no3 from(
        select reg_no,bind_no,row_number()over(partition by reg_no order by bind_no)rn
          from t_bind)
        group by reg_no  
      

  10.   

    还是得谢谢啦
    我现在只能mysql下做试验,能不能不用某种特定数据库的东西.
      

  11.   

    没用过mysql
    不知道哪些通用...
    麻烦在bind_no的序号上
    不能用分析函数..
    看看别人有什么好办法
      

  12.   

    select m.reg_no, 
    (select min(bind_no) from t_bind where reg_no=m.reg_no) bind_no1, 
    (select min(bind_no) from t_bind where reg_no=m.reg_no and bind_no>(select min(bind_no) from t_bind where reg_no=m.reg_no)) bind_no2,
    (select max(bind_no) from t_bind where reg_no=m.reg_no and (select count(*) from t_bind  where reg_no=m.reg_no)>2) bind_no3 
    from t_bind as m group by m.reg_no;