3 way join SQL

Posted on 2008-10-27
Last Modified: 2012-05-05
I'm trying to connect 3 tables in my database: organizations, category_values and users_categories. If I change this:
where (uc.uid = '3' AND cv.catId='163')
to this:
where (uc.uid = '3')
I get all of the results I would expect. But adding the cv.catId I get 0 results (query runs with no errors). Is there something structurally wrong with my query?
$query = "select distinct o.orgId,, cv.catId

	from (organizations o join category_values cv

	on cv.orgId = o.orgId) join users_categories uc

	on uc.catId = cv.catId

	where (uc.uid = '3' AND cv.catId='163')

	order by ASC";

Open in new window

Question by:level9wizard
  • 3
  • 2
LVL 11

Author Comment

ID: 22815236
As some more info, when I print row data the catId is the same for every row result. As if it's taking it from uc.catId and not cv.catId
LVL 142

Accepted Solution

Guy Hengel [angelIII / a3] earned 500 total points
ID: 22819994
what about this:
$query = "select distinct o.orgId,, cv.catId

        from organizations o join category_values cv

        on cv.orgId = o.orgId 

        left join users_categories uc

        on uc.catId = cv.catId and uc.uid = '3' AND cv.catId='163'

        order by ASC";

Open in new window

LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22819997
otherwise, you might not have any rows matching both criteria?!
LVL 11

Author Comment

ID: 22821413
I got the answer I was looking for thru IRC, thought i'd share...

$query = "SELECT o.orgId,, cv.catId FROM organizations o join (
SELECT DISTINCT cv2.orgId FROM users_categories uc JOIN category_values cv2 on uc.catId=cv2.catId WHERE uc.catId=200 and uc.uid=3 ) as d ON d.orgId=o.orgId
JOIN category_values cv ON d.orgId=cv.orgId
WHERE cv.catId='163'";
LVL 11

Author Closing Comment

ID: 31510383
The real solution to my problem is below, but I want to award you points for trying. Thanks!

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been using r1soft Continuous Data Protection ( for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now