?
Solved

Constructing a Selective Join Query

Posted on 2010-01-10
4
Medium Priority
?
316 Views
Last Modified: 2013-12-12
Hi All,
I have two tables,
one called people which records a person's base details, like name and address, and which includes the following fields
PLrn - the record number
PLemail - the person's email address
PLsubscribe - the subscription flag

and one called peoplelink which sets their membership level, which can be different for each subdomain they belong to, and which contains the following fields
PKprn - the person's record number from the people table
PKsubdomain - the subdomain they are a member of
PKlevel - the membership level

I want to write a single query that joins these two together so that I get the email address [from the people table] and the membership level [from the peoplelink table] ONLY for members who belong to a nominated subdomain.

I have tried the following, but it doesn't work, and I suspect is completely wrong...
SELECT * FROM people WHERE people.PLsubscribe = 'Y'
  LEFT JOIN peoplelink ON peoplelink.PKprn = people.PLrn WHERE peoplelink.PKsubdomain = 'somesubdomain';

I'm having a great deal of difficulty getting my head around this one... the complication, to me, is that there's conditional selection from both tables.

Any assistance would be gratefully accepted.

Cheers
CTB
0
Comment
Question by:chestertbear
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:pauly5
ID: 26280784
Hi There,

There should only be a single WHERE clause for the query, which needs to go to the end.

Try this:

SELECT *FROM   people LEFT JOIN peoplelink ON peoplelink.PKprn = people.PLrn
WHERE peoplelink.PKsubdomain = 'somesubdomain' AND people.PLsubscribe = 'Y'
0
 
LVL 1

Accepted Solution

by:
pauly5 earned 400 total points
ID: 26280787
Additional...
The join type should be INNER JOIN not LEFT JOIN if you only want the rows for the specified subdomain and where their subscription is enabled.
I.e.

SELECT * FROM   people INNER JOIN peoplelink ON peoplelink.PKprn = people.PLrn
WHERE peoplelink.PKsubdomain = 'somesubdomain' AND people.PLsubscribe = 'Y'
0
 
LVL 41

Assisted Solution

by:Sharath
Sharath earned 100 total points
ID: 26280984
Instead of * in the SELECT clause of the query provided by pauly5, you can mention the columns of your choice.


SELECT PLEmail,PKLevel FROM   people INNER JOIN peoplelink ON peoplelink.PKprn = people.PLrn
WHERE peoplelink.PKsubdomain = 'somesubdomain' AND people.PLsubscribe = 'Y'
0
 

Author Closing Comment

by:chestertbear
ID: 31675348
Brilliant.  Thank you both so much.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

777 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