Solved

3 way join SQL

Posted on 2008-10-27
5
245 Views
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, o.name, 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 o.name ASC";

Open in new window

0
Comment
Question by:level9wizard
  • 3
  • 2
5 Comments
 
LVL 11

Author Comment

by:level9wizard
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
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 22819994
what about this:
$query = "select distinct o.orgId, o.name, 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 o.name ASC";

Open in new window

0
 
LVL 142

Expert Comment

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

Author Comment

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

$query = "SELECT o.orgId, o.name, 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'";
0
 
LVL 11

Author Closing Comment

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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 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