第一个问题:有A B两个表A表:id              name    
货物id         货物name有以下记录
id          name
1           货物1
2           货物2
3           货物3B表:id        goods_id     beginDate   endDate         price
主键       货物id      开始日期    结束日期        价格有以下记录
id        goods_id     beginDate   endDate         price
1             1        20100101    20100202        200
2             1        20100203    20100303        123
3             2        20100109    20100209        300
4             2        20100210    20100220        301
5             3        20100107    20100207         112
6             3        20100208    20100220        223
问题:
现在 $beginDate 和 $endDate 是参数, 要求查询列出 $beginDate 和 $endDate 之间的各货物的最高价格例如,当   $beginDate = 20100111  $endDate = 20100210时,
查询结果为:1  货物1   200
2  货物2   301
3  货物3   223
第二个问题:
题目和上面一样,只是参数不一样,有$beginDate 和 $endDate   ,$minPrice 和 $maxPrice
要求查询列出$beginDate 和 $endDate 之间,价格又在$minPrice 和 $maxPrice之间的货物。例如,当   $beginDate = 20100111  $endDate = 20100210时, $minPrice=100 $maxPrice=200时,
查询结果为:1    货物1
3    货物3

解决方案 »

  1.   


    select a.id,a.name,max(b.price)
    from a inner join b on a.id=b.goods_id
    where $beginDate<=endDate and $endDate>=beginDate
    group by a.id,a.name;mysql> select * from a;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    1 | 货物1 |
    |    2 | 货物2 |
    |    3 | 货物3 |
    +------+-------+
    3 rows in set (0.00 sec)mysql> select * from b;
    +------+----------+-----------+----------+-------+
    | id   | goods_id | beginDate | endDate  | price |
    +------+----------+-----------+----------+-------+
    |    1 |        1 |  20100101 | 20100202 |   200 |
    |    2 |        1 |  20100203 | 20100303 |   123 |
    |    3 |        2 |  20100109 | 20100209 |   300 |
    |    4 |        2 |  20100210 | 20100220 |   301 |
    |    5 |        3 |  20100107 | 20100207 |   112 |
    |    6 |        3 |  20100208 | 20100220 |   223 |
    +------+----------+-----------+----------+-------+
    6 rows in set (0.00 sec)mysql> set @beginDate = 20100111;
    Query OK, 0 rows affected (0.00 sec)mysql> set @endDate = 20100210;
    Query OK, 0 rows affected (0.00 sec)mysql> select a.id,a.name,max(b.price)
        -> from a inner join b on a.id=b.goods_id
        -> where @beginDate<=endDate and @endDate>=beginDate
        -> group by a.id,a.name;
    +------+-------+--------------+
    | id   | name  | max(b.price) |
    +------+-------+--------------+
    |    1 | 货物1 |          200 |
    |    2 | 货物2 |          301 |
    |    3 | 货物3 |          223 |
    +------+-------+--------------+
    3 rows in set (0.00 sec)mysql>
      

  2.   

    select a.id,a.name
    from a inner join b on a.id=b.goods_id
    where $beginDate<=endDate and $endDate>=beginDate
    and price between $minPrice and $maxPrice
    group by a.id,a.name;
    mysql> set @beginDate = 20100111;
    Query OK, 0 rows affected (0.00 sec)mysql> set @endDate = 20100210;
    Query OK, 0 rows affected (0.00 sec)mysql> set @minPrice=100 ;
    Query OK, 0 rows affected (0.00 sec)mysql> set @maxPrice=200;
    Query OK, 0 rows affected (0.00 sec)mysql>
    mysql> select a.id,a.name
        -> from a inner join b on a.id=b.goods_id
        -> where @beginDate<=endDate and @endDate>=beginDate
        -> and price between @minPrice and @maxPrice
        -> group by a.id,a.name;
    +------+-------+
    | id   | name  |
    +------+-------+
    |    1 | 货物1 |
    |    3 | 货物3 |
    +------+-------+
    2 rows in set (0.00 sec)mysql>
      

  3.   

    ACMAIN_CHM,太强悍了! 太感谢您了!