小弟对sql不熟,求各位大侠指教

解决方案 »

  1.   

    select 
    cityname
    from
    (
    select 
    a.city cityname,
    count(a.city) city_cnt
    from
    city a
    inner join
    user b
    on
    a.id=b.usercity
    group by
    1
    )
    order by
    city_cnt
      

  2.   

    select b.city, count(a.id) count_num from user a,city b
    where a.usercity = b.city
    order by count_num
      

  3.   

    select b.city, count(a.id) count_num from user a,city b
    where a.usercity = b.city
    group by b.city
      

  4.   

    select a.city, b.idcount
    from city a
        inner join ( select usercity, count(id) AS idcount
                     from user 
                     group by usercity) b
           ON a.city = b.usercity
    order by b.idcount desc;
      

  5.   

    我觉的user表中的usercity与city表的id是城市的id 号吧,由于写的不明确,不知道如何关联了
    select b.city,count(a.id) from user a ,city b
      where a.usercity(+)=b.id
      

  6.   

    忘了写group by了
    我觉的user表中的usercity与city表的id是城市的id 号吧,由于写的不明确,不知道如何关联了
    select b.city,count(a.id) from user a ,city b
      where a.usercity(+)=b.id
    group by  b.city
      

  7.   

    select city.city
      from city,
           (select distinct first_value(user.usercity) over(order by count(user.id) desc)) usercity
      from user
     group by user.usercity) aa
     where city.id = aa.usercity
    如果usercity存放的是表city的id值就用上面的
    如果usercity存放的是表city的city值就用下面的
    select city.city
      from city,
           (select distinct first_value(user.usercity) over(order by count(user.id) desc)) usercity
      from user
     group by user.usercity) aa
     where city.city = aa.usercity