ulisses Posted August 24, 2009 Share Posted August 24, 2009 hi guys! i'm a begginner on sql and php, i have to do something and i don't know how. i have a table, with:- a personal number that identifies each subscriber- the class of service to which the person subscribes- the date of the subscriptionnow i'm planing to create another table containing statistical informations, such as:- the number of subscriptions for a day- how many clients subscribed to each class of service, etc.i think of using sql count() function, but don't know how to implement it. i don't know if i've made u understand my problem, but i'd appreciate any kind of help. thank u Link to comment Share on other sites More sharing options...
chibineku Posted August 24, 2009 Share Posted August 24, 2009 I'm also new to (My)SQL, so if someone with more experience answers, take theirs above mine, but the easiest way is to make a SELECT query and then count the num_rows - for example:$query = "SELECT * FROM table_name WHERE date_subscribed = 'whatever'";$query_res = mysqli_query($mysqli_connection, $query);$count = mysqli_num_rows($query_res);Just swap date_subscribed for any other field you like. Link to comment Share on other sites More sharing options...
ulisses Posted August 24, 2009 Author Share Posted August 24, 2009 Thanks for your reply, that's what i was thinking about. I'll try it. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 24, 2009 Share Posted August 24, 2009 You can use COUNT like this:SELECT COUNT(*) AS num FROM tableThe field in the result will be called "num" and will contain the number of rows in the table. You can add a WHERE clause to that if you want. You can also use the other functions like SUM the same way:SELECT SUM(field) AS num FROM tableFunctions like COUNT and SUM are called aggregate functions. You can use GROUP BY with an aggregate function to get other statistics. If you have a table called subscriptions that contains a field called service, you can use this to get the number of subscriptions for each service:SELECT service, COUNT(service) AS num FROM subscriptions GROUP BY serviceThat will return one record for each service, where each record contains a service field that lists the service, and a num field that lists the number of subscriptions. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.