[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

SQL INNER JOIN using 'IN' rather than '='

Posted on 2011-10-19
4
Medium Priority
?
242 Views
Last Modified: 2012-05-12
This query works great.
SELECT     mr.mailRecipient_ID, mr.mailRecipient_lists, mr.mailRecipient_firstName, mr.mailRecipient_email, mr.mailRecipient_lastMsg
FROM         mailRecipient AS mr INNER JOIN
                      mailList AS ml ON ml.mailList_ID IN (4, 5, 6)

Open in new window



However...  this is what I really want to do.  The mailRecipient_lists column of the mailRecipient table contains a comma delimited list of id's from the mailList table (mailList_ID).  How do I select a list of mailList_titles with each record associate to the id's in the in the mr.mailRecipient_lists column?  Obviously the query below is not going to work.  :)  TIA
SELECT     mr.mailRecipient_ID, mr.mailRecipient_lists, mr.mailRecipient_firstName, mr.mailRecipient_email, mr.mailRecipient_lastMsg, ml.mailList_id, ml.mailList_title
FROM         mailRecipient AS mr INNER JOIN
                      mailList AS ml ON ml.mailList_ID IN (mr.mailRecipient_lists)

Open in new window

0
Comment
Question by:brianmfalls
  • 2
4 Comments
 
LVL 7

Expert Comment

by:mmr159
ID: 36992519
You're almost there.  Use a subquery.


SELECT     mr.mailRecipient_ID, mr.mailRecipient_lists, mr.mailRecipient_firstName, mr.mailRecipient_email, mr.mailRecipient_lastMsg, ml.mailList_id, ml.mailList_title
FROM         mailRecipient AS mr INNER JOIN
                      mailList AS ml ON ml.mailList_ID IN (
                            SELECT mailRecipient_lists
                            FROM mailRecipient
                            )
0
 
LVL 7

Expert Comment

by:mmr159
ID: 36992572
> The mailRecipient_lists column of the mailRecipient table contains a comma delimited list of id's from the mailList table (mailList_ID).

Sorry.  I just realized my previous post will not work due the data being comma delimited.  You will need to parse this out first.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 36992604
you will need to do something like this, presuming that the mailinglist field is a comma-delimited field:
http://www.experts-exchange.com/A_1536.html
it will be the CROSS APPLY dbo.ParmsToList() method ...
0
 
LVL 1

Author Closing Comment

by:brianmfalls
ID: 36992930
Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview

826 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