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;
Related
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.
I got a database that registers user actions and their geolocation.
Now I would like to fetch this data at the hand of the last action per user.
The table looks a bit like:
geoaction_id AUTO INCREMENT
geoaction_user
geoaction_creationdate (Y-m-d H:i:s)
geoaction_action
geoaction_lon
geoaction_lat
Now I would like to make a simple query that selects of all users the last item.
But LIMIT 0,1 just parses one row no matter what. (LOGICALLY!!)
Group by gives a little better result.
But how to get only the last item per user?
Try this, please provide the queries you have checked out so far, in order to assist you better.
SELECT geoaction_user, geoaction_action
FROM table-name
GROUP BY geoaction_user
ORDER BY geoaction_action DESC LIMIT 1
Working with sets:
SELECT
g.geoaction_user,
g.geoaction_action,
g.geoaction_creationdate,
g.geoaction_lat,
g.geoaction_lon
FROM
(
SELECT
geoaction_user,
MAX(geoaction_id) max_id
FROM
geoactions
GROUP BY geoaction_user
) s
JOIN
geoactions g
ON s.geoaction_user = g.geoaction_user
AND s.max_id = geoaction_id
The subquery generates a virtual table with the geoaction_id from the latest entry in the tabble for each user_id, then the table is joined to get the data belong to the latest id.
If you need to filter out some records place the where clause in the subquery
I have a web application that stores points in a table, and total points in the user table as below:
User Table
user_id | total_points
Points Table
id | date | user_id | points
Every time a user earns a point, the following steps occur:
1. Enter points value to points table
2. Calculate SUM of the points for that user
3. Update the user table with the new SUM of points (total_points)
The values in the user table might get out of sync with the sum in the points table, and I want to be able to recalculate the SUM of all points for every user once in a while (eg. once a month). I could write a PHP script that could loop through each user in the user table and find the sum for that user and update the total_points, but that would be a lot of SQL queries.
Is there a better(efficient) way of doing what I am trying to do?
Thanks...
A more efficient way to do this would be the following:
User Table
user_id
Points Table
id | date | user_id | points
Total Points View
user_id | total_points
A view is effectively a select statement disguised as a table. The select statement would be: SELECT "user_id", SUM("points") AS "total_points" FROM "Points Table" GROUP BY "user_id". To create a view, execute CREATE VIEW "Total Points View" AS <SELECT STATEMENT> where SELECT STATEMENT is the previous select statement.
Once the view has been created, you can treat it as you would any regular table.
P.S.: I don't know that the quotes are necessary unless your table names actually contain spaces, but it's been a while since I worked with MySQL, so I don't remember it's idiosyncrasies.
You have to user Triggers for this, to make the users total points in sync with the user_points table. Something like:
Create Trigger UpdateUserTotalPoints AFTER INSERT ON points
FOR EACH ROW Begin
UPDATE users u
INNER JOIN
(
SELECT user_id, SUM(points) totalPoints
FROM points
GROUP BY user_id
) p ON u.user_id = p.user_id
SET u.total_points = p.totalPoints;
END;
SQL Fiddle Demo
Note that: As noted by #FireLizzard, if these records in the second table, are frequently updated or delted, you have to have other AFTER UPDATE and AFTER DELETE triggers as well, to keep the two tables in sync. And in this case the solution that #FireLizzard will be better in this case.
If you want it once a month, you can’t deal with just MySQL. You have too « logic » code here, and put too logic in database is not the correct way to go. The trigger of Karan Punamiya could be nice, but it will update the user_table on every insert in points table, and it’s not what you seem to want.
For the fact you want to be able to remove points, just add bsarv new negated rows in points, don’t remove any row (it will break the history trace).
If you really want it periodically, you can run a cron script that does that, or even call your PHP script ;)
here's the scenario. I am generating a report of all the members who have dues to pay for a certain time period.
I am successfully selecting and displaying each database entry as a row in a html table.
The problem is the total fields the report must have. Each member pays different amounts based on what services they use, so I must add the values in each field individually to ensure proper result.
Question is, how do I go about adding the rows/field together?
Edit:
To clarify. I am adding dues paid and donations paid fields. They are classified and integer in mysql database.
Example, let's say that my query returns 3 results. I wish to add the dues paid for all 3 results and display it as total_dues_paid. Same idea for donations.
This must be dynamic for any given number of results, as this changes month to month and we have seen several hundred results in some months.
Thanks
To add fields (columns):
SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM table;
To add rows together, use the SUM() aggregate:
SELECT
userid,
SUM(col1) AS col1_total,
SUM(col2) AS col2_total
FROM table
GROUP BY userid
You can add in your query string.
SELECT (field1 + field2) AS Total
FROM table
I have a table of customers with a 1 recorded against their customerid on different dates.
I would like to find the sum of the 1's recorded in descending order. I'm using MySQL and php
Thanks
My guess is that you want the sum of records marked with 1 per customer and sort that result in descending order? If so, the following should do the trick :
select cust.id, sum(cone.one) as number_ones
from customers as cust
inner join customer_ones as cone on cone.id=cust.id
group by cust.id
order by number_ones desc
This is assuming that 'one' is the column containing ones (and only contains 0 or 1 - otherwise you will have to add WHERE cone.one = 1), customers is your customer table and customer_ones is the table containing your customer data.
As i get you right, this is simple sql request what u need:
SELECT COUNT(id) as total from customers
Just make in php:
$sql="SELECT COUNT(id) from customers";
$query=mysql_query($sql) or die(mysql_error());
$res=mysql_fetch_assoc($query);
$summ=$res['total']; //<- Your summ (i.e. quantity of rows in table)
Btw, you can use mysql_num_rows instead.
Or explain please more accurately what output you need. To make sorting by date or any other dependency you will need to make other request using WHERE clause and date comparison.