![]() | |
#1
| |||
| |||
|
#2
| |||||
| |||||
|
|
Hey everyone, I'm working on a project that will let the users store their budget, and in the backbone I have a MySQL database. However, I'm not really fluent in SQL. I have two questions: The first is a request for a suggestion of a SQL query. I have a table that contains all user entries in the following format: id member_id date amount 1 1 070210 2.3 2 1 070210 4.3 3 2 070211 5.8 4 2 070212 1.6 5 1 070213 3.2 |
|
In order to sum up the total amount for a specific member, I use the the following query: SELECT SUM(amount) as amount FROM budget WHERE member_id=1 Now how do I solve this if I'd like to limit the SUM to count the amount in entries between specific id's? The reason for this is that I have a table that shows every member's expenses. The number of rows Displayed? |
|
can be set by the member and is taken care of with a LIMIT command in that function, but the same method isn't working in this query. Any ideas of how to solve this? |
|
Then I have a "follow-up" question. When I use the SUM function on the amounts above, I end up with a number with loads of decimals when I'd only like one. Right now I'm solving this with a ROUND command. The types of the column is FLOAT. Any ideas? |
|
Thanks a lot in advance. |
#3
| |||
| |||
|
|
Now how do I solve this if I'd like to limit the SUM to count the amount in entries between specific id's? The reason for this is that I have a table that shows every member's expenses. The number of rows can be set by the member and is taken care of with a LIMIT command in that function, but the same method isn't working in this query. Any ideas of how to solve this? |
|
Then I have a "follow-up" question. When I use the SUM function on the amounts above, I end up with a number with loads of decimals when I'd only like one. Right now I'm solving this with a ROUND command. The types of the column is FLOAT. Any ideas? |
#4
| |||
| |||
|
|
On Mar 10, 7:11 pm, "Alex" <alex.wald... (AT) gmail (DOT) com> wrote: [...] id member_id date amount 1 1 070210 2.3 2 1 070210 4.3 3 2 070211 5.8 4 2 070212 1.6 5 1 070213 3.2 First I suggest you learn some relational database design. The id column in your table is not necessary. A better Primary key looks to be the combined member_id and date. |
#5
| |||
| |||
|
|
it will not be possible to use date in the key. |
#6
| |||
| |||
|
|
Then I have a "follow-up" question. When I use the SUM function on the amounts above, I end up with a number with loads of decimals when I'd only like one. Right now I'm solving this with a ROUND command. The types of the column is FLOAT. Any ideas? |
|
Do the formatting on the client. Otherwise I think ROUND will do the job (eventhough I dont know what ROUND does using mysql). |
#7
| |||
| |||
|
|
Ed Prochak wrote: On Mar 10, 7:11 pm, "Alex" <alex.wald... (AT) gmail (DOT) com> wrote: [...] id member_id date amount 1 1 070210 2.3 2 1 070210 4.3 3 2 070211 5.8 4 2 070212 1.6 5 1 070213 3.2 First I suggest you learn some relational database design. The id column in your table is not necessary. A better Primary key looks to be the combined member_id and date. In general I agree, but in this case id 1 and 2 would violate the suggested primary key. Unless each member can do only one *transaction* per date, it will not be possible to use date in the key. BTW, date is all kind of things in sql (type, function for starters), hence not a really good name for a column. /Lennart [...] |
![]() |
| Thread Tools | |
| Display Modes | |
| |