Pus*_*dra 5 php mysql select zend-framework
在我的项目中,我正在使用一个名为"Compensation"的表,就像......
+--------------------------------------------------------------+
| Id | receiver_Id | compensation | date |
|--------------------------------------------------------------|
| 1 | 5 | 50% | 2011-02-15 12:15:00 |
| 2 | 3 | 40% | 2011-04-05 18:35:00 |
| 3 | 3 | 30% | 2011-04-25 06:24:00 |
| 4 | 5 | 45% | 2011-04-21 19:05:00 |
| 5 | 5 | 60% | 2011-04-30 12:05:00 |
.......................
Run Code Online (Sandbox Code Playgroud)
在这里date represents that the compensation is changed on that particular date.因为receiver 5,从2011年2月15日12:15:01到2011年4月21日19:05:00` ,赔偿率为50%before Feb 15 2011. And the compensation is45%.等等....fromthe date
在这里当我创建invoice了一个月APRIL 2011的receiver 5,我必须使用compensation as 45%直到日期21 April 2011和22 April 2011 to 30 April 2011 I have to use 60%作为补偿......
但如何获得一个月或两个日期之间的补偿,因为补偿可能会在一个月内多次修改,因为id 4和5显示.......
请帮我writing SQL为高于或我必须作出changes对table structure使它简单......?
提前致谢
当您的表跟踪有效日期和有效日期时,这将更容易:
+--------------------------------------------------------------+---------------------+
| Id | receiver_Id | compensation | date_from | date_to |
|--------------------------------------------------------------|---------------------|
| 1 | 5 | 50% | 2011-02-15 12:15:00 | 2011-04-21 19:05:00 |
| 2 | 3 | 40% | 2011-04-05 18:35:00 | 2011-04-25 06:24:00 |
| 3 | 3 | 30% | 2011-04-25 06:24:00 | 0000-00-00 00:00:00 |
| 4 | 5 | 45% | 2011-04-21 19:05:00 | 2011-04-30 12:05:00 |
| 5 | 5 | 60% | 2011-04-30 12:05:00 | 0000-00-00 00:00:00 |
.......................
Run Code Online (Sandbox Code Playgroud)
然后你可以查询:
SELECT * FROM compensations
WHERE (date_to > $start_of_month OR date_to = 0 )
AND date_from < $end_of_month;
Run Code Online (Sandbox Code Playgroud)