I would like you know how I can sum up all values from a single table column based on a specific constraint. What I mean is this, imagine a table called payments, the payments table has three columns; ID, type of payment and amount. ID is not a primary key so it can duplicate. So let's say there are four entries under one particular ID and I want to get the total for amount for that ID. How can I do it? Thought a while loop would work but tried everything I could think of and it didn't. Please help! Final school project
If you are using mysql you can use the sum function as follows:
SELECT SUM(t1.ID) FROM db1.tblmoney AS t1 WHERE t1.ID = '1';
This will select the sum total of column "ID" in all rows within db1.tblmoney with the ID of 1. If on the other hand you are trying to get a count of the rows with that ID you can use this:
SELECT COUNT(t1.*) FROM db1.tblmoney AS t1 WHERE t1.ID = '1';
The different in these two statements is SUM with get the added values of all ID
So if you have 2 rows of id ='1' you will get 2 if you have two rows of id = '2' you will get 4
Whereas with Count() it will count the number of rows matching your select: 2 rows of id ='1' you will get 2 if you have two rows of id = '2' you will get 2.
Related
I am writing a complex MySQL query. My actual query is more complex than I mentioned below.
I have a table named example and columns are id, name, option_1, option_2 . Of course id column is PK . I want to retrieve like this:
SELECT `id`,`name`,count(`option_1`),count(`option_2`)
My problem is I want to use "GROUP BY `id`" for count(`option_1`) and "GROUP BY `name`" for count(`option_2`) respectively. Now I have to break down it into multiple code in my php code.
How can I achieve what I want in a single query?
What you're asking for doesn't make a ton of sense. You want option 1 grouped by id and option 2 grouped by name, and you want to show all four columns in one result set.
First of all, if id is a primary key, then the count will just be the number of rows in the table since there will be no duplicate ids.
Second, even if id wasn't a primary key, and there were duplicate values, the grouping is different, so the counts represented in your result set would be grouped incorrectly.
Given that id is a primary key, perhaps your intention is actually to get a total count of the rows in the table. Perhaps this query would suit your needs?
SELECT
name,
COUNT(option_2) AS options
FROM
example
GROUP BY
name
UNION ALL
SELECT
'Total' AS name,
COUNT(*) AS options
FROM
example
This should get you a count of all the option_2 values, grouped by name, with the final row having a name of 'Total' and the count being the total number of rows in the table.
Aside from that, I'm not sure you're going to find the answer you're looking for due to the problems you would encounter matching up groupings.
I'm trying to figure out a solution to a query.
Question :
I need to find the sum of rows which have the same value in a certain column. From this I then need to echo out certain bits of information along with the total count each value is seen (i.e. if 'project_id' has two rows with the same value as 11122 then the total count will be 2). I don't know the values I will need as these will be random project numbers in my table.
The column name I need to count by is 'project_id'.
I can't figure out how to echo in the following way:
Project ID = 11122 Total Reviews = 2
Project ID = 99999 Total Reviews = 5
Thanks!
As mentioned above in the comments, Sean's answer works a treat!
SELECT project_id, COUNT(*) as TotalReviews FROM YourTable GROUP BY project_id
i need some help with my MySQL statement. i want to call the data from my MySQL table with specific condition. and i wish to present the data into a chart by using php.
For example, here is my table.
11003 trid(primary key) 1(tid) Adventure(name) US(location) 2014(date) 5(duration) 1003(id) romex(fname) jen(lname) approved(pending)
11006 trid(primary key) 1(tid) Adventure(name) US(location) 2014(date) 5(duration) 1006(id) siew(fname) siew(lname) approved(pending)
21003 trid(primary key) 2(tid) Yourfuture(name) US(location) 2014(date) 1(duration) 1003(id) romex(fname) jen(lname) approved(pending)
i want the data to count the total of people will going in each training. and list the name out.
Expected result is something like this.
Adventure romex jen
siew siew total 2
Yourfuture romes jen total 1
the pending can be 'decline' or 'approved' when approved mean they are going.
my current MySQL statement is this.
SELECT name, date, fname, lname FROM `trainingrequest` WHERE pending = 'Approved'
You can count the total people in one of two ways:
select count(*) as c from trainingrequest where pending = 'Approved'
This will return the total number of approved records. To narrow it down you will need to use an event id or such
select count(*) as c from trainingrequest where pending = 'Approved' and name = 'adventure';
or you could just grab all the data you need and count the rows in php. The first method will require two calls to the database. Counting the rows in PHP will require one call to the database!
It looks like you also want to GROUP BY your data
select * from trainingrequest where .... GROUP BY location;
I have two tables.
One contains default data for 3 columns, (value is 1 or 0) In the same table is a ClientID
Another table contains edited data with a date, ClientID and a extra column named 'Changed'
If Changed = 1 then the values in the 3 columns are changed and therefore need to be read from the second table.
This al works fine, but I want to make a report in php where a daterange can be selected and a query should group by ClientID and count all 1's in the selected daterange of the 3 columns. (Each column seperate)
Here's the trick: When Changed = 0 in the specific row then It should check the default value and if Changed = 1 it should check the second table. And then count it with the previous rows.
I hope you understand what I want to create
You can use IF function from SQL
For example
SELECT SUM(IF(Changed=1, t1.col1, t2.col2)) FROM t1, t2 WHERE t1.id=t2.id
This is example how you can use columns for case in SUM
I have a simple SQL Query:
SELECT tid,
COUNT(*) AS bpn
FROM mark_list
WHERE userid = $userid
GROUP BY tid
Now the column tid is basically a category list associated with each entry. The categories are unique numeric values.
What I am trying to do is get an overall count of how many records there as per userid, but I only want to count an entire category one time (meaning if category 3 has 10000 records, it should only receive a count of 1).
The caveat is that sometimes the category is listed as null or sometimes a 0. If the item has either a 0 or a null, it has no category and I want them counted as their own separate entities and not lumped into a single large category.
Wheeee!
SELECT SUM(`tid` IS NULL) AS `total_null`,
SUM(`tid` = 0) AS `total_zero`,
COUNT(DISTINCT `tid`) AS `other`
FROM `mark_list`
WHERE `user_id` = $userid
Edit: note that if total_zero is greater than 0, you will have to subtract one from the "other" result (because tid=0 will get counted in that column)
You can alter the query to not take into account those particular values (via the WHERE clause), and then perhaps run a separate query that ONLY takes into account those values.
There may be a way to combine it into only one query, but this way should work, too.