MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 13th, 2012, 08:00 PM
caplocks caplocks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 caplocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

TABLE users
user_id
user_name

users VALUES
(1,'John Doe),
(2,'Jane Smith),
(3,'Kevin Brown');

TABLE pos
pos_id
pos_date
pos_start_amount
pos_end_amount
pos_start_userID
pos_end_userID

pos VALUES
(1,'2012-09-05 00:00:00',100,320,1,1),
(2,'2012-09-15 00:00:00',230,543,1,3),
(3,'2012-10-14 00:00:00',40,56,2,3),
(4,'2012-11-03 00:00:00',24,124,2,1),
(5,'2012-11-25 00:00:00',20,75,3,3);

TABLE pos_transfer
pos_id
transfer_1
transfer_2
transfer_3
transfer_1_userID
transfer_2_userID
transfer_3_userID

pos_transfer VALUES
(1,20,'','',2,'',''),
(2,45,70,'',1,3,''),
(3,50,120,20,2,2,1),
(4,43,'','',3,'',''),
(5,24,30,'',2,3,'');

Thank you

Reply With Quote
  #2  
Old November 14th, 2012, 10:53 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);

$num_users = number of user in both tables?
Comments on this post
Jacques1 disagrees: bad advice; counting rows is what COUNT(*) is for

Reply With Quote
  #3  
Old November 14th, 2012, 11:02 PM
caplocks caplocks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 caplocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Thank You

Reply With Quote
  #4  
Old November 14th, 2012, 11:11 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
Quote:
Originally Posted by caplocks
Yes that's exactly what I want to do. Can you write a sample query for me to see.

Thank You


What language are you writing the Query in?

Reply With Quote
  #5  
Old November 14th, 2012, 11:11 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,033 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
Hi,

do not use mysql_num_rows() to count rows. That's the most unclean and slow way to do this. Counting rows is what MySQL is for:

Code:
SELECT COUNT(*) ...

Reply With Quote
  #6  
Old November 14th, 2012, 11:31 PM
caplocks caplocks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 caplocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #7  
Old November 14th, 2012, 11:51 PM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #8  
Old November 15th, 2012, 12:34 AM
caplocks caplocks is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 4 caplocks User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 5 sec
Reputation Power: 0
Quote:
Originally Posted by BitZoid
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.


Thanks BitZoid I will give this a try.

Reply With Quote
  #9  
Old November 15th, 2012, 02:56 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 16 m 32 sec
Reputation Power: 4141
Quote:
Originally Posted by BitZoid
$user_count = mysql_num_rows ($resultUsers);
this is grossly inefficient

use COUNT(*) in sql to count rows
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #10  
Old November 15th, 2012, 05:03 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,033 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
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.

Reply With Quote
  #11  
Old November 15th, 2012, 07:49 AM
BitZoid's Avatar
BitZoid BitZoid is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 98 BitZoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 2 h 20 m 38 sec
Reputation Power: 1
oh yeah, I'm in SQL Database class now in school and I am still using my old outdated methods.

SELECT user_id, COUNT(user) AS [User Count] FROM table 1 INNER JOIN table 2 ON table1.user=table2.user GROUP BY user_id

Does this look right?

Reply With Quote
  #12  
Old November 15th, 2012, 09:38 AM
r937's Avatar
r937 r937 is online now
SQL Consultant
Dev Shed God 43rd Plane (26000 - 26499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,442 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 14 h 16 m 32 sec
Reputation Power: 4141
Quote:
Originally Posted by BitZoid
Does this look right?
not in mysql, no

those square brackets are a microsoftism and will work only in sql server and access

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > MySQL query help

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap