Discuss MySQL query help in the MySQL Help forum on Dev Shed. MySQL query help MySQL Help forum discussing administration, SQL syntax, and other MySQL-related topics. MySQL is an open-source relational database management system (RDBMS).
Posts: 4
Time spent in forums: 33 m 5 sec
Reputation Power: 0
MySQL query help
I need help to write a query to count the total of a user in 2 tables. Count total based on userID in the following fields by month pos_start_userID, pos_end_userID, transfer_1_userID, transfer_2_userID, transfer_3_userID.
For example: The total pos count for John Doe in the month of September is 3, for October is 1, and for November is 1.
Posts: 98
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Are you just trying to count the amount of times a user is in 2 tables? If so you can just use a query = "SELECT statement with a JOIN and WHERE criteria of user_id = 'user_id'
and then: $num_users = mysql_num_rows ($queryResult);
Posts: 4
Time spent in forums: 33 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by BitZoid
Are you just trying to count the amount of times a user is in 2 tables? If so you can just use a query = "SELECT statement with a JOIN and WHERE criteria of user_id = 'user_id'
and then: $num_users = mysql_num_rows ($queryResult);
$num_users = number of user in both tables?
Yes that's exactly what I want to do. Can you write a sample query for me to see.
Posts: 4
Time spent in forums: 33 m 5 sec
Reputation Power: 0
BitZoid Im using PHP.
@ Jacques1: Thanks for the concern but so far BitZoid is the only one that have a solution for me. I'm learning more and more each day and I will keep this tip in mind.
Posts: 98
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by caplocks
BitZoid Im using PHP.
@ Jacques1: Thanks for the concern but so far BitZoid is the only one that have a solution for me. I'm learning more and more each day and I will keep this tip in mind.
No clean solutions for you, but I can guide you
$queryUsers = "SELECT user FROM table1, table 2 WHERE user='criteria'";
$resultUsers = mysql_query($queryUsers);
$user_count = mysql_num_rows ($resultUsers);
Note** This uses EquiJoin which is being depricated, instead of the newer more relevant Inner Join.
Posts: 2,033
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
Quote:
Originally Posted by r937
this is grossly inefficient
use COUNT(*) in sql to count rows
I already said that. But since nobody else posted a solution, they sticked with it.
@ caplocks:
I don't understand the "pos" logic. What do "pos" and "pos_transfer" with their columns mean? And how exactly do you count the users?
In any case, you'll need to JOIN the tables (like BitZoid said), GROUP them by the user ID, the MONTH() and the YEAR() and then select the COUNT(*) to calculate the number of rows for each user, month and year.