第一个问题:有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
货物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
解决方案 »
- MySQL客户端中文显示乱码?急急急!!!
- 请高手解决一个关于MYSQL CLUSTER的问题!急!!!!
- mysql5.0的bin目录里缺少PDB文件,PDB文件的用途是?
- Mysql一条判断检索的问题。高手来看看
- 求两条查询优化
- 请问这条命令哪里写错了?谢谢,涛涛:)
- 数据库表设计
- 送分来了,LINUX下,怎么执行MYSQL,进入命令提示符理,我启动后,执行另外一个MYSQL脚本,说我权限拒绝,怎么办?
- mysql 是部署有master-slave、双master、cluster模式,请问哪一种模式比较好?
- 一个复杂的关系多表关系检索,请大侠指导
- 多线程操作同一结果集需不需要同步
- 如何设置要求开发者的java程序不许链接,只能查看数据。
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>
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>