User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MySQL section within the Web Development category of DaniWeb, a massive community of 397,645 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,408 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MySQL advertiser:
Views: 626 | Replies: 5 | Solved
Reply
Join Date: Aug 2007
Location: Cape Town, South Africa
Posts: 6
Reputation: fabzster is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fabzster fabzster is offline Offline
Newbie Poster

Help Mysql Joins Assistance

  #1  
Dec 13th, 2007
Hi

I would really appreciate some assistance on the following as I suck a bit when it comes to joins

i have 3 tables consultants,points,orders

all tables have the column id_number

I need to run a query that will give me a list of all the consultants with a total from points and a total from orders

when I do a left join with 2 tables then I get the correct answer, the minute I add the 3rd I get incorrect data

the query:

SELECT
consultants.name,
consultants.surname,
consultants.cell_number,
consultants.register_status,
consultants.id_number,
Sum(points.points_added),
Sum(orders.points_cost)
FROM
consultants
Left Join points ON points.id_number = consultants.id_number
left Join orders ON orders.id_number = consultants.id_number
GROUP BY
consultants.name,
consultants.surname,
consultants.cell_number,
consultants.register_status,
consultants.id_number

I thing the problem lies in that some consultants do not have any orders and may not have been allocated points

your assistance will be greatly appreciated
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Mysql Joins Assistance

  #2  
Dec 13th, 2007
You can use, sum(ifnull(points,0), points_added). If the 1st expression in ifnull ie., points is null, it returns 0 and if its not null, it returns points value. I too dont know much about joins and related problems, but, this link might be useful.

http://dev.mysql.com/doc/refman/5.0/...functions.html
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 238
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Mysql Joins Assistance

  #3  
Dec 13th, 2007
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Aug 2007
Location: Cape Town, South Africa
Posts: 6
Reputation: fabzster is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fabzster fabzster is offline Offline
Newbie Poster

Re: Mysql Joins Assistance

  #4  
Dec 13th, 2007
thanks for replying I will give it a try and let u know
Reply With Quote  
Join Date: Aug 2007
Location: Cape Town, South Africa
Posts: 6
Reputation: fabzster is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fabzster fabzster is offline Offline
Newbie Poster

Solution Re: Mysql Joins Assistance

  #5  
Dec 14th, 2007
Hi

I found the answer on another forum hope this helps someone out

SELECT consultants.name
     , consultants.surname
     , consultants.cell_number
     , consultants.register_status
     , consultants.id_number
     , pts.pts_added as points_added
     , ords.pts_cost as points_cost
  FROM consultants
LEFT OUTER
  JOIN ( SELECT id_number 
              , SUM(points_added) AS pts_added
           FROM points
         GROUP
             BY id_number ) AS pts
    ON pts.id_number = consultants.id_number
LEFT OUTER
  JOIN ( SELECT id_number 
              , SUM(points_cost) AS pts_cost
           FROM orders
         GROUP
             BY id_number ) AS ords
    ON ords.id_number = consultants.id_number
Reply With Quote  
Join Date: Sep 2007
Location: North Bay Ontario
Posts: 174
Reputation: trudge is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 20
trudge trudge is offline Offline
Junior Poster

Re: Mysql Joins Assistance

  #6  
Dec 15th, 2007
Originally Posted by fabzster View Post
Hi
I found the answer on another forum hope this helps someone out


Then please flag this thread as 'SOLVED' in the first posting.
Amer Neely - Web Mechanic
"Others make web sites. We make web sites work!"
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MySQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MySQL Forum

All times are GMT -4. The time now is 10:49 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC