more on sql! |
This is a discussion on more on sql! within the General Development Issues forums, part of the Development category; Hiya guys, I have a quick SQL question, I have a database of rental prices that have different values for ... |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Member Join Date: Jan 2009 Location: Spain
Posts: 179
Thanks: 6
Thanked 15 Times in 14 Posts
![]() Expertise: PHP Experience: Professional |
Hiya guys, I have a quick SQL question, I have a database of rental prices that have different values for different days. What I ideally need to do is calculate the full rental price within an SQL query, so for example: Code: add up all "prices" from "prices table" between "from date" and "to date" for the item being rented Thanks! Stu |
| | |
| | #2 |
![]() Join Date: Dec 2008 Location: Kent Uk
Posts: 1,458
Blog Entries: 5 Thanks: 13
Thanked 43 Times in 43 Posts
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Expertise: (X)HTML / CSS Experience: Professional |
I am not sure on the syntax, but cant you just do PHP Code: |
| | |
| | #3 |
| Member Join Date: Jan 2009 Location: Spain
Posts: 179
Thanks: 6
Thanked 15 Times in 14 Posts
![]() Expertise: PHP Experience: Professional |
Hi Saltedm8, Thanks for taking a look! Unfortunately I need the code in SQL as I need to be able to order by price, which is where I'm having the problem! I might not have explained myself properly, sorry! For example, I have a rental table: Code: id item 1 Rental Item 1 2 Rental Item 2 etc... Code: id rental_id from to price 1 1 2010-01-01 2010-03-31 9.99 2 1 2010-04-01 2010-06-30 15.99 etc.... I shall put my thinking cap on as I'm not sure the way I want to do it is possible!! Stu Last edited by stu2000; 1st December 2009 at 10:14 AM.. Reason: lining up tables lol |
| | |
| | #4 |
![]() Join Date: Dec 2008 Location: Kent Uk
Posts: 1,458
Blog Entries: 5 Thanks: 13
Thanked 43 Times in 43 Posts
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Expertise: (X)HTML / CSS Experience: Professional | |
| | |
| | #5 |
| Member Join Date: Jan 2009 Location: Spain
Posts: 179
Thanks: 6
Thanked 15 Times in 14 Posts
![]() Expertise: PHP Experience: Professional |
Hiya, Thanks for the link, I was thinking that's the route to go, however from that page I stole and amended the following code: Code: SELECT SUM(`price`) as "Total Price" FROM rental_prices WHERE `rental_id` = '1' AND `from` >= '2010-03-30' AND `to` <= '2010-04-05'; I think it needs a 'while' statement or something like that, to go: Code: set date = '2010-03-30';
while(date < '2010-04-05'){
SELECT `price` FROM rental_prices WHERE `from` >= 'date'
AND `to` <= 'date'; //ie find the price for that date only
TotalPrice = TotalPrice + `price`; //increment total price
increase `date` by 1 day; // increment the date to go to start of while clause and find next days rental price.
}
Stu |
| | |
| | #6 |
| Member Join Date: Jan 2009 Location: Spain
Posts: 179
Thanks: 6
Thanked 15 Times in 14 Posts
![]() Expertise: PHP Experience: Professional |
Ignore that first bit, I'm being an idiot as usual! lol I got the more than/less than symbols the wrong way round, the query should have been: Code: SELECT SUM(`price`) as "Total Price" FROM rental_prices WHERE `rental_id` = '1' AND `from` <= '2010-03-30' AND `to` >= '2010-04-05'; Code: SELECT SUM( `price` ) AS "Total Price" FROM rental_prices WHERE `rental_id` = '1' AND `from` <= '2010-03-30' UNION SELECT SUM( `price` ) AS "Total Price" FROM rental_prices WHERE `rental_id` = '1' AND `to` >= '2010-04-05' But again I think I'm on a wild goose chase here, neigh mind! thanks for your help anyway mate! Stu |
| | |
| | #7 |
![]() Join Date: Dec 2008 Location: Kent Uk
Posts: 1,458
Blog Entries: 5 Thanks: 13
Thanked 43 Times in 43 Posts
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Expertise: (X)HTML / CSS Experience: Professional |
I think the best people to talk to would be either davidj or cloudedvision.. its definitely a little out of my league lol send them a pm, see if you get a response |
| | |
| | #8 |
| Member Join Date: Jan 2009 Location: Spain
Posts: 179
Thanks: 6
Thanked 15 Times in 14 Posts
![]() Expertise: PHP Experience: Professional |
Thanks mate, out of my league too... I like my PHP but I've never really got on with SQL!! I think I need to take some time out and do a mini course or something! Anyhow, luck has had it that the site owner has now decided that he wants just one daily price for the whole year for each rental item... phew! I dodged the bullet on that one! lol |
| | |
![]() |
| Thread Tools | |
| Display Modes | |