Mitjana Posted November 28, 2012 Share Posted November 28, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271300-selecting-last-cells-of-one-table-and-first-few-of-another/ Share on other sites More sharing options...
Barand Posted November 28, 2012 Share Posted November 28, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271300-selecting-last-cells-of-one-table-and-first-few-of-another/#findComment-1395952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.