例如:我现在有这样两个价格表A表和B表,结构都一样
id int(10) 主键
goods_id int(10) 货物id
price varchar(10) 货物价格
date varcahr(10) 日期
A表 例如有以下数据:
id goods_id price begin_date end_date
1 001 201.6 20100409 20100506
2 001 109.7 20100507 20100517
3 001 1001.3 20100518 20100701B表 例如有以下数据
id goods_id price begin_date end_date
1 001 100.6 20100309 20100416
2 001 250.7 20100417 20100517现在需要一段SQL,查询指定的$goos_id, 在 $begin_date日期 到 $end_date日期 之间的最低价(这两个价格表的所有价格)!例如: 如果 $goos_id =001, $begin_date = 20100407, $end_date =20100416, 那么出来的将是B表的 100.6;
如果 $goos_id =001, $begin_date = 20100417, $end_date =20100505, 那么出来的将是A表的 201.6;
如果 $goos_id =001, $begin_date = 20100517, $end_date =20100520, 那么出来的将是A表的109.7; 目的就是,获得指定日期段的,在两个价格表中的 最低价格。谢谢!
id int(10) 主键
goods_id int(10) 货物id
price varchar(10) 货物价格
date varcahr(10) 日期
A表 例如有以下数据:
id goods_id price begin_date end_date
1 001 201.6 20100409 20100506
2 001 109.7 20100507 20100517
3 001 1001.3 20100518 20100701B表 例如有以下数据
id goods_id price begin_date end_date
1 001 100.6 20100309 20100416
2 001 250.7 20100417 20100517现在需要一段SQL,查询指定的$goos_id, 在 $begin_date日期 到 $end_date日期 之间的最低价(这两个价格表的所有价格)!例如: 如果 $goos_id =001, $begin_date = 20100407, $end_date =20100416, 那么出来的将是B表的 100.6;
如果 $goos_id =001, $begin_date = 20100417, $end_date =20100505, 那么出来的将是A表的 201.6;
如果 $goos_id =001, $begin_date = 20100517, $end_date =20100520, 那么出来的将是A表的109.7; 目的就是,获得指定日期段的,在两个价格表中的 最低价格。谢谢!
解决方案 »
- 百万级别limit的优化
- Incorrect string value: '\xAD\xBF The...' for column '
- 触发器可以实现禁止用户删除某个表数据?
- mysql如何将数据倒入excel,或者将excel的数据倒入mysql
- 大家知道哪些比较大的公司都使用MySql吗?
- MYSQL中有关复合搜索,索引的问题
- 查询mysql连接状态(在线等)
- 一个Sum(a*b)的问题
- 我用MySQL和Tomcat做JSP,配置数据源后,能够向数据库中插入数据,但是不能够查询出来!
- 求助,mysql 远程访问连接失败
- mysql 不支持子查询 。那应该如何实现查询啊。
- select count(*) vs list.size()
select min(price) from
(select * from A where begin_date>20100407 and end_date<20100416
union all
select * from B where begin_date>20100407 and end_date<20100416
) c
详细说明结果是怎样得出的
(select * from tA
union all
select * from tB
) c
where begin_date>='20100407' and end_date<='20100416 '
or
end_date>='20100417' and begin_date<='20100417'select min(price) from
(select * from tA
union all
select * from tB
) c
where begin_date>='20100407' and end_date<='20100416 '
or
end_date>='20100407' and begin_date<='20100407'
from
(
select price from A表 where $begin_date<=end_date and $end_date>=begin_date
union all
select price from B表 where $begin_date<=end_date and $end_date>=begin_date
) t
from
(
select price from A表 where $begin_date<=end_date and $end_date>=begin_date and $goods_id='001'
union all
select price from B表 where $begin_date<=end_date and $end_date>=begin_date and $goods_id='001'
) t
from
(
select price from A表 where $begin_date<=end_date and $end_date>=begin_date and goods_id=$goos_id
union all
select price from B表 where $begin_date<=end_date and $end_date>=begin_date and goods_id=$goos_id
) t条件直接在加内表上比较好。加在T上效率则会比较差 比如下面的。select min(price)
from
(
select price from A表
union all
select price from B表
) t
where $begin_date<=end_date and $end_date>=begin_date and goods_id=$goos_id
(select * from tA
union all
select * from tB
) c
where begin_date>=$begin_date and end_date<=$end_date
or
end_date>= $begin_date and begin_date<= $begin_date