SELECT h.id, h.name, r.id, r.roomtype, MIN(price) AS MMP FROM roomtype r left JOIN hotel h ON r.hotelID=h.id GROUP BY h.id, h.name, r.id, r.roomtype试试

解决方案 »

  1.   

    SELECT h.id, h.name, r.id, r.roomtype, r.price AS MMP 
    FROM hotel h left join(
    select r.*
    from roomtype r,(select price=min(price) from roomtype group by hotelid)r1
    where r.price=r1.price 
    )b on r.hotelid=h.id
      

  2.   

    --写错了别名,改一下:SELECT h.id, h.name, r.id, r.roomtype, r.price AS MMP 
    FROM hotel h left join(
    select r.*
    from roomtype r,(select price=min(price) from roomtype group by hotelid)r1
    where r.price=r1.price 
    )r on r.hotelid=h.id
      

  3.   

    inner join 不能实现
    用LEFT OUTER JOIN 就OK
      

  4.   

    --测试--测试数据
    create table hotel(id int,name varchar(10))
    insert hotel select 1,'金陵饭店'
    union  all   select 2,'电子大厦'
    union  all   select 3,'白宫大酒店'create table roomtype(id int,hotelid int,roomtype varchar(10),price int)
    insert roomtype select 1,2,'标准间'  ,125
    union  all      select 2,2,'豪华套间',380
    union  all      select 3,2,'普通间'  ,80
    union  all      select 4,1,'标准间'  ,200
    union  all      select 5,3,'标准间'  ,160
    go--查询
    SELECT h.id, h.name, r.id, r.roomtype, r.price AS MMP 
    FROM hotel h left join(
    select r.*
    from roomtype r,(select price=min(price) from roomtype group by hotelid)r1
    where r.price=r1.price 
    )r on r.hotelid=h.id
    go--删除测试
    drop table hotel,roomtype/*--测试结果id          name       id          roomtype   MMP         
    ----------- ---------- ----------- ---------- ----------- 
    1           金陵饭店       4           标准间        200
    2           电子大厦       3           普通间        80
    3           白宫大酒店      5           标准间        160(所影响的行数为 3 行)
    --*/
      

  5.   

    --如果存在同一hotelid,最低price有多个,则改用:
    SELECT h.id, h.name, r.id, r.roomtype, r.price AS MMP 
    FROM hotel h left join(
    select r.*
    from roomtype r,(
    select id=min(id)
    from roomtype r,(select price=min(price) from roomtype group by hotelid)r1
    where r.price=r1.price 
    group by hotelid
    )r1 where r.id=r1.id
    )r on r.hotelid=h.id
      

  6.   

    LEFT JOIN和LEFT OUTER JOIN都不行
      

  7.   

    declare @tb1 table(myid int,myname varchar(20))
    insert into @tb1
    select 1,'金陵饭店'union all
    select 2,'电子大厦'union all
    select 3,'白宫大酒店'declare @tb2 table(myid2 int,hotelid int,roomtype varchar(20) ,price int)
    insert into @tb2
    select 1,2,'标准间',125 union all
    select 2,2,'豪华间',380 union all
    select 3,2,'普通间',80 union all
    select 4,1,'标准间',200 union all
    select 5,3,'标准间',160select h.myid,h.myname,r3.roomtype,r3.price as mmp from @tb1 h left join (select r1.* from @tb2 r1 , (select price=min(price) from @tb2 group by hotelid)r2 where r1.price=r2.price)r3 on h.myid=r3.hotelid
      

  8.   

    select h.myid,h.myname,r3.roomtype,r3.price as mmp from hotel h left join (select r1.* from room r1 , (select price=min(price) from room group by hotelid)r2 where r1.price=r2.price)r3 on h.myid=r3.hotelid
      

  9.   

    TO:zhangfeng7398()还是不行!
    显示数据每两个重复一次啊!