select count(user_id) from (select distinct user_id from 表1)//这句查的是从表1中查出不同的user_id
select count(*) from (select * from 表2 where product_name is not null order by product_bussiness_count desc) where user_id={0} and sort_id=11 and (rownum<2)这句是根据条件查询
我现在想做的是:加了一个AspNetPage控件,之前没加分页时,实现的是先查出不同的user_id,然后再根据user_id和sort_id查出产品(每个用户只提取1个产品),但现在加上分页,就不知道怎么做了,能不能把上面的两句话合成一句?

解决方案 »

  1.   

    这里用count(*)是想做什么,这两个count(*)难道是合并两个数量?!
    还是从product_name以user_id和sort_id为条件为每个用户抓一个产品出来?
      

  2.   


    --你的第一句 select count(user_id) from (select distinct user_id from 表1)
    --你的第二句 select count(*) from (select * from 表2 where product_name is not null order by product_bussiness_count desc) where user_id={0} and sort_id=11 and (rownum<2)
    --你说一个用户提取一个产品 那可不可以理解为 表2中的sort_id和表1中的user_id一一对应?
    --你是不是就是要统计 不同的user_id对应的你第二句查询结果的总数量?
    --那么可以这么做
    select count(*) 
    from (select * 
          from 表2 
          where product_name is not null 
          order by product_bussiness_count desc) 
    where user_id in (select count(user_id) from (select distinct user_id from 表1)) 
    and sort_id=11 
    and rownum<2
    --中间标出了一段就是合并修改的
      

  3.   

    不是,这些都是一个表中的,就是表中有很多产品,有些就是一个用户发好几个产品,我现在只取不同的用户且product_name不能为空,因为我分页的时候,第一句查出来的是70条,但有些product_name为空,实际上的条数是30条,但分页的时候它读取的条数是70,这样就不对了!
      

  4.   

    select rownum,c.*
    from 
    (
    select a.user_id
           ,b.*
           ,row_number() over(partition by a.user_id order by b.product_bussiness_count desc) rn
    from 表1 a,表2 b 
    where a.user_id = b.user_id  and b.product_name is not null and b.ort_id=11
    ) c
    where rn = 1