用ORACLE查询,先简单说明一下资源:
两张表,假如一张表为:orders(订单表),主键:orderid(订单编号);一张表为:orderlist(订单明细表),主键;listid,这两张表能过orderid(订单编号)关联起来,订单
下面我描述一下我的需求:
我想查出订单表(orders)中一个订单编号(orderid)对应订单明细表(orderlist)中的订单明细记录有多少条?
比如:
数据要这样列出:
orderid            listid              count(listid)
081230000001       0812300000010001    3
081230000001       0812300000010002    3
081230000001       0812300000010003    3
081230000002       0812300000010011    2
081230000002       0812300000010012    2
081230000003       0812300000010021    1请问一下,要实现这样的需求结果,查询SQL该怎么写?

解决方案 »

  1.   

    select a.*, b.listid from (
      select orderid,count(listid) from orderlist group by orderid
    )a, orderlist b where a.orderid=b.orderid
      

  2.   

    select a.orderid,b.listid,(select count(*) from a_list where orderid =a.orderid) from a_order a,a_list b
    where a.orderid =b.orderid
      

  3.   

    将*改为1,效率好点
    select a.orderid,b.listid,(select count(1) from a_list where orderid =a.orderid) from a_order a,a_list b 
    where a.orderid =b.orderid
      

  4.   


    SQL codeselect a.*, b.listid from (
      select orderid,count(listid) from orderlist group by orderid
    )a join orderlist b on(a.orderid=b.orderid)
      

  5.   

    4楼,*改为1的话,如果第一列不是主键或者非空的话。这个COUNT(1)是不准确的。。
      

  6.   

    试试这个,如果可以告诉我select orderid ,
           listid,
           count(listid) over(partition by orderid ) as num
      from t_orders  
      

  7.   

    不好意思,看错了。呵呵,select a.orderid,
           b.listid,
           count(b.listid) over(partition by a.orderid) as num
      from orders a, orderlist b
     where a.orderid = b.listid
      

  8.   

    select a.orderid,b.listid,count(b.listid) from orders a,orderlist b
    where 1=1
    and a.orderid = b.orderid
    group by a.orderid,b.listid这样应该也可以的(注明,我目前没有测试过)
      

  9.   

    count(listid) over(partition by orderid )先进行分组吗?
      

  10.   

    来晚了7楼正解,楼主随便到google上搜下oracle over什么都有了