现在我有两个表
表a:
order_id  tel
a中现有一个订单有两条记录
aaa-1000 1580000000
aaa-1000 1580000001
表b:
order_id  pp_id
b中对应订单号也有两条记录
aaa-1000 888888
aaa-1000 999999想要查询出
aaa-1000 1580000000 888888
aaa-1000 1580000001 999999
这样的查询结果sql语句该如何写?

解决方案 »

  1.   

    select a.order_id,a.tel,b.pp_id from a a,b b where a.order_id=b.order_id 
      

  2.   

    楼上的写法是有问题的,这样会得出4条记录
    select a.order_id,a.tel,b.pp_id from 
    (select order_id,tel,rownum num from a)ta,
    (select order_id,pp_id,rownum num form b) tb
     where ta.order_id=tb.order_id and ta.num=tb.num
      

  3.   

    麻烦了点  用内连接,楼上的麻烦了点select a.order_id,tel,pp_id from a join b on a.order_id=b.order_id;
    这个能实现
      

  4.   

    使用rownum这个方法我也尝试过,如果表中只有这两条记录是可以的,如果表中有大量订单,使用rownum关联就不合适了,这种情况该怎么处理?
      

  5.   

    感谢各位的回答,解决办法已找到,使用row_number() over()函数——select a.order_id,m_id,tel from
    (select order_id, m_id ,row_number() over(order by order_id) rn from order_mid) a
    left join (select order_id, tel ,row_number() over(order by order_id) rn from order_tel) b on a.order_id=b.order_id and a.rn=b.rn