If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Joining 2 Tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-03-08, 01:40
BlueGemini BlueGemini is offline
Registered User
 
Join Date: Jun 2008
Posts: 49
Joining 2 Tables

Could somebody please help me with this....

I'm about to join 2 tables...

Table1
User NoOfPrintsInP1 Time
--------------------------------------------
James 5 18:00
James 6 18:30
Joshua 1 18:35
Mo 5 18:55

Table2
User NoOfPrintsInP2 Time
--------------------------------------------
James 1 15:00
James 2 15:05
Joshua 3 15:30
James 5 16:00

I suppose to do a query that outputs the total number of both NoOfPrints per User. It should be something like this...

User NoOfPrintsInP1 NoOfPrintsInP2
-----------------------------------------------------
James 11 8
Joshua 1 3
Mo 5 0

I did a Inner Join but it didn't output the way I expect and the numbers are wrong... what went wrong? any ideas?

Last edited by BlueGemini; 07-03-08 at 01:45.
Reply With Quote
  #2 (permalink)  
Old 07-03-08, 08:19
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT User
     , SUM(p1) AS NoOfPrintsInP1
     , SUM(p2) AS NoOfPrintsInP2
  FROM (
SELECT User
     , SUM(NoOfPrintsInP1) AS p1
     , 0 AS p2
  FROM Table1
GROUP BY User
UNION ALL
SELECT User
     , 0 
     , SUM(NoOfPrintsInP2) AS p2
  FROM Table2
GROUP BY User
) AS d
GROUP BY User
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On