表1  id title content
1 哈哈 嘿嘿
2 哈哈 嘿嘿
表2
id aid jiage
1  1  300
2  1  500
3  1  200
4  2  500
5  2  600表一和表二是一个多的关系
我现在想要的结果是id title content jiage
1  哈哈 嘿嘿  200
2  哈哈 嘿嘿  500然后在这个结果中我想做个价格的比较,比如只显示 100 到 200 这个中间的行,怎么弄,发sql版半天没人回答,做程序做到这了,郁闷啊、、、

解决方案 »

  1.   

    select 表1.id,表1.title,表1.content,表2.jiage from 表1,表2 where 表1.id=表2.aid and 表2.jiage>=100 and 表2.jiage<=500以上为选出显示 100 到 500 这个中间的行。
      

  2.   

    其实就是想要表1和表2结合下,只要表2的jiage最小的那个,集合成一个表后,然后在选择下jiage这个、、
      

  3.   

    select * from 表1 A left join 表2 B where A.id = B.aid and B.jiage >= 100 and B.jiage<=200 
      

  4.   

    SELECT A.id, A.title, A.content, MIN(B.jiage) AS jiage
    FROM   dbo.表1 AS A WITH(NOLOCK)
    INNER JOIN dbo.表2 AS B WITH(NOLOCK) ON A.id = B.aid 
    GROUP BY A.id, A.title, A.content 
    HAVING B.jiage < 200 AND B.jiage > 100
      

  5.   


    --是不是要找出最小jiage?--
    select a.*,jiage from tb1 a inner join (select min(jiage) jiage,aid from tb2 group by aid) b on a.id=b.aid
    /*
    1 哈哈 嘿嘿 200
    2 哈哈 嘿嘿 500
    */