6

Possible Duplicate:
Calculating total quantity of equipments for a date range

Project: I am working on a project which is about some rooms and equipments using in the rooms. The software is about scheduling the equipments in the rooms. In other words, it is a reservation software that reserves selected equipments in separate rooms for needed dates and times ranges. I have many tables in MYsSQL database working with Php but I will mention the tables my question is about. The tables I will relate my questions are equipment table (Table A), schedule table (Table B) and equipments using in the related schedule (Table C).

Table A: equipment list table

+------+----------+-----------+
| eqid | eqName   | available | 
+------+----------+-----------+
|    1 | book     |        90 |      
|    2 | pen      |        82 | 
|    3 | computer |        25 |
+------+----------+-----------+    

In table A; eqid represents unique id of an equipment, eqName represents name of an equipment, available represents total available equipments existing.

Table B: schedule table

+------------+------------+------------+-----------+----------+--------+
| scheduleid | startDate  | endDate    | startTime | endTime  | office |
+------------+------------+------------+-----------+----------+--------+
|          1 | 2012-08-27 | 2012-08-27 | 08:30:00  | 10:00:00 | room1  |
|          2 | 2012-08-27 | 2012-08-27 | 09:30:00  | 11:00:00 | room3  |
|          3 | 2012-08-28 | 2012-08-30 | 08:30:00  | 12:00:00 | room2  |
|          4 | 2012-08-29 | 2012-08-31 | 11:30:00  | 14:00:00 | room1  |
|          5 | 2012-08-28 | 2012-08-28 | 10:30:00  | 14:00:00 | room3  |
|          6 | 2012-08-27 | 2012-08-30 | 08:30:00  | 10:00:00 | room4  |
|          7 | 2012-08-27 | 2012-08-27 | 10:30:00  | 12:00:00 | room4  |    
|          8 | 2012-08-27 | 2012-08-30 | 08:30:00  | 11:00:00 | room6  |
|          9 | 2012-08-27 | 2012-08-27 | 10:30:00  | 12:00:00 | room5  | 
+------------+------------+------------+-----------+----------+--------+    

In table B; scheduleid represents unique id for a schedule, startDate and endDate are date range for a schedule, startTime and endTime time range for a schedule, office means that where the schedule will take place. Let me give an example here. Scheduleid 1 means there is a reservation on 27th of august 2012, Monday and it is from 08.30 to 10:00. As it start and end on same day this is just one day reservation in room1. However, Scheduleid 3 means there is a reservation starts on 28th of august 2012, Tuesday and goes on until 30th of august 2012, Thursday at 08:30-12:00... in other words, it lasts for 3 days and everyday from 08:30 to 12:00... So there is a reservation from Tuesday to Thursday at 08:30 to 12:00 in room2... I hope this is clear.

Table C: equipments using in the related schedule

+--------+------------+------+-------------+
| Autoid | scheduleid | eqid | amountInSch |
+--------+------------+------+-------------+
|      1 |          1 |    1 |           2 |      
|      2 |          1 |    2 |           3 |
|      3 |          1 |    3 |           1 |
|      4 |          2 |    1 |           1 |
|      5 |          2 |    2 |           1 |
|      6 |          2 |    3 |           2 |
|      7 |          3 |    2 |           1 |
|      8 |          3 |    3 |           3 |
|      9 |          4 |    2 |           1 |
|     10 |          4 |    3 |           1 |
|     11 |          5 |    1 |           1 |
|     12 |          6 |    1 |           1 |
|     13 |          6 |    3 |           2 |
|     14 |          6 |    2 |           4 |
|     15 |          7 |    1 |           5 |
|     16 |          7 |    2 |           6 |
|     17 |          8 |    2 |           1 |
|     18 |          9 |    1 |           8 |
|     19 |          9 |    2 |           5 |  
|     20 |          9 |    3 |           6 |  
+--------+------------+------+-------------+

In table C: Autoid represents unique automatic id generated by auto-increment, scheduleid comes from Table B, eqid comes from Table A, amountInSch represents how many (amount) equipment will use in the related schedule. I want to give an example here. Scheduleid 1 in Table C, there are 3 rows. This means that scheduleid 1 related in TAble B will use 2 books (eqid 1), 3 pens (eqid 2) and 1 computer (eqid 3) in room1 specified dates and times in table B . Another example is that scheduleid 3 in Table C is related 2 rows. It means that 1 pen (eqId 2) and 3 computers (eqId 3) will be using in room2 from 27th to 30th of august 2012 everyday from 08:30 to 12:00.

The above is the explanation and give some information about the project. The table rows are not permanent. When you make a reservation, there will be a new row in Table B and if it is selected an equipment, there will be new rows in table C...

The Question:

I want to calculate left amount of a specific equipment when I supply eqId, startDate, endDate, startTime and endTime...

An example:

eqId: 1 (book)

startDate: 2012-08-27

endDate: 2012-08-27

startTime: 08:30:00

endTime: 12:00:00

Result should be: 14 books used in schedule and 76 left available books

Because: if you look scheduleIds and related eqIds, you will only see 1, 2, 6, 7, 9 scheduleIds related to my query(dates and eqId). If you sum the all amount of related in Table C, you will get the wrong result. In other words, related amounts for eqId(1-book) and for 1, 2, 6, 7, 9 scheduleIds are 2, 1, 1, 5, 8 respectively. So if you sum them you will get 17 which is wrong. Because, 1 and 9 schedule don't intersect each other in terms of start and end Times, and 6 and 7 don't intersect each other either. as a result of them 2 stays lonely and can be count separately. We must consider 1 and 9 as summed 8 because 8 is bigger than 2. it is same for 6 and 7, considered as 5 because of 5 is bigger than 1...

So folks! I am not sure how I can sum/ this in programming algorithm. Is there a way to do in SQL or do I have to use PHP and Mysql together? and How?

SQLFiddle Records

4

1 回答 1

0

有不同的方法可以实现这一点——我会指出一些可以与大多数 DB 引擎一起使用的东西。

在 mysql 中,可以在单个查询中合并表。你需要的是这样的东西:

$eqid = 1;
$startDate = "2012-08-27";
$endDate = "2012-08-27";
$startTime = "08:30:00";
$endTime = "12:00:00";
$sql = "SELECT SUM(`amountInSch`) FROM `table_c`,`table_b` WHERE `eqid` = $eqid 
    AND `startDate` >= '$startDate' AND `startDate` <= '$endDate' AND `endDate` 
    <= '$endDate' AND `endDate` >= '$startDate' AND `startTime` >= '$startTime' 
    AND `startTime` <= '$endTime' AND `endTime` <= '$endTime' AND `endTime` >=
    '$startTime' AND `table_b`.`scheduleid` = `table_c`.`scheduleid`";
$r = mysql_query($sql);
$n = mysql_result($r,0,0);

诀窍是使用table_b`.`scheduleid` = `table_c`.`scheduleid (由于此处的标记而缺少一些引号)来合并两个表。

于 2012-08-27T07:31:42.530 回答