Jump to content

Selecting Last Cells Of One Table And First Few Of Another


Mitjana

Recommended Posts

Hi. Please bare with me my knowledge of PHP and MSQL is very limited

 

Table layout, MYSQL version: 5.5

Date is PHP strtotime format

Cost_x is ££.dd format

Cost_x = daily cost

 

Table 2012 [last 5 days/cells]

+----+-----------------+---------+---------+---------+

| ID | date | cost_a | cost_b | cost_c |

+----+------------- ---+---------+---------+---------+

|361 | 1344376800 | 15.50 | 18.70 | 22.80 |

|362 | 1344463200 | 15.50 | 18.70 | 22.80 |

|363 | 1344549600 | 15.50 | 18.70 | 22.80 |

|364 | 1344636000 | 18.50 | 20.70 | 25.80 |

|365 | 1345759200 | 18.50 | 20.70 | 25.80 |

+----+-----------------+---------+---------+---------+

 

Table 2013 [first 5 days/cells]

+-----+-----------------+---------+---------+---------+

| ID | date | cost_a | cost_b | cost_c |

+-----+------------- ---+---------+---------+---------+

| 001 | 1356994800 | 18.50 | 20.70 | 25.80 |

| 002 | 1357081200 | 18.50 | 20.70 | 25.80 |

| 003 | 1357167600 | 15.50 | 18.70 | 22.80 |

| 004 | 1357254000 | 15.50 | 18.70 | 22.80 |

| 005 | 1357340400 | 15.50 | 18.70 | 22.80 |

+-----+-----------------+---------+---------+---------+

 

I have a small car hire co and am trying to update the website to calculate the hire costs.

 

So far I have the basics working :-

 

SELECT SUM(`cost_a`) as total_a

FROM `carhire_cost_2012`

WHERE `date_strtotime`

BETWEEN 1344376800 AND 1345759200

 

And/Or

 

SELECT SUM(`cost_a`) as total_a

FROM `carhire_cost_2013`

WHERE `date_strtotime`

BETWEEN 1356994800 AND 1357167600

 

What I would like to have is to SUM the cost_x when the dates chosen cross over to the next year E.G. BETWEEN 1344376800 AND 1345759200 [last few days of 'this year' and first few days of 'next year']

 

Any help greatfully received

If you need to do that, why on earth are you separating your data into different tables?

 

And mysql DATE format is far more flexible than the format you are using.

 

The answer is select those you need from 1st table and UNION with select from 2nd table.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.