Jump to content

[SOLVED] joins and conditional logic


aunquarra

Recommended Posts

Okay, I have two tables I frequently join. One is a list of people, and the other is a list of details about those people.

 

I typically do the joins with the id number in the people table and a column with people IDs in the details table.

 

So it would be like...

 

SELECT `people`.*, `details`.* from `people` left join `details` on `people`.`id`=`details`.`people_id`....

 

The trick is, as you might guess by the way I'm building it, each people_id could have multiple rows in the details table. Typically, I only call on the details table when I'm looking at one specific person, so no biggie there. But in this case, I need to retrieve a list of people.

 

What I'm wanting to accomplish is to, without resource-heavy sub-selects, return only people who don't have a specific detail.

 

So like, if I'm looking for people who are unemployed, and the way I tell if a person is employed is to check and see if they have a row in the details table that has a `detail_category`='job', then to return a list of unemployed people, I have to check to see if they don't have such row.

 

The more I think about it, the more I think this isn't going to be something I can do with a simple join. Either I'm going to have to have PHP do the legwork or do a subselect. But I figured I'd ask the professionals...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.