php - Combining sum() of similar entry's, and including a WHERE clause SQL
Get the solution ↓↓↓I am trying to add up the the numbers from amount, if the booking_id matches. I have been having trouble with summing these numbers as well as adding in a WHERE clause.
This is my code so far
At this time it is summing the numbers correctly, although if I add in the where it doesn't show any results
select *, Sum(amount) FROM payment group by booking_id where booking_id = 1
I have tried some methods like adding queries in queries but I have had no luck. The results I'm attempting to get are below.
booking_id amount
Output should be:
{-code-3}
I am attempting to group the results so that booking_id with a value of 1 will return a sum of 5+6.
My goal is to be able to add together the amounts that have the same booking id. As well as to be able to include a WHERE clause within this query
Answer
Solution:
To get all booking_id's as requested, simply skip theWHERE
clause:
select booking_id, Sum(amount)
FROM payment
group by booking_id
To get all booking_id's for a specific ident (as later requested), add theWHERE
clause:
select booking_id, Sum(amount)
FROM payment
where client_id = 123
group by booking_id
Answer
Solution:
Where should be before group by
select booking_id , Sum(amount) FROM payment
where booking_id = 1
group by booking_id
Answer
Solution:
you did wrong only in order to write sql
select booking_id, Sum(amount) FROM payment
where client_id = 1 -- your condition
group by booking_id
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: illegal string offset
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.