[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Selecting all entries from one table and only associated entries from another

Posted on 2011-03-17
4
Medium Priority
?
200 Views
Last Modified: 2012-05-11
Hi,

I have two tables in a database and I wish to select all entries from one table but only certain entries, which are associated by ID, from the second. I've tried using a JOIN but so far all I have been able to figure out how to do is join the tables and therefore select the two entries where both tables match.

Here's the code so far:

 
SELECT C.ConfigDescription, D.DDI, C.ConfigDefault
FROM tConfigurations C
JOIN tDDIs D ON D.ConfigurationID = C.ID
WHERE D.AccountID = 1307

Open in new window


'1307' is for testing purposes is a hard coded value of a variable that's being passed in through a stored procedure.

What I want to do is select all the "D.DDI"s from the tDDIs table regardless of whether or not they have associated entries with the tConfigurations table. And where entries do have associated entries I want to display the "C.ConfigDescription" and "C.ConfigDDefault" from the the tConfigurations table.

Thanks in advance guys.
0
Comment
Question by:Lilliman
[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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35158110
delete with join:

DELETE c
FROM tConfigurations C
JOIN tDDIs D ON D.ConfigurationID = C.ID
WHERE D.AccountID = 1307 

Open in new window


is that what you need


0
 

Author Comment

by:Lilliman
ID: 35158278
Sorry, I don' think I explained myself very well. I don't want to delete any of the date. What I want to do is select all entries in one table and only the associating entries in another table, e.g.

Let's say tDDIs has 4 entries in it:

ID     AccountID     ConfigurationID        DDI
1           1307                   1                      0207blahblahblah
2           1307                   1                      0208blahblahblah
3           1307                    NULL              0800blahblahblah
4           1307                    NULL              0900blahblahblah

And the tConfigurations also has 4 entries in it:

ID      AccountID     ConfigDescription           ConfigDefault

1           1307             blahblahblah1            0115blahblahblah
2           1307             blahblahblah2             0116blahblahblah
3           1307             blahblahblah3              0870blahblahblah
4           1600             somethingelse            0871blahblahblah

What I want to do is select all "DDI"s from tDDIs that match the AccountID "1307" whether they have a ConfigurationID or not. Then I want to join the ones that do with the tConfigurations table where tDDIs ConfigurationID = tConfigurations ID.

So it should look something like this:

ConfigDescription              DDI                      ConfigDefault
blahblahblah1                   0207blahblahblah           0115blahblahblah
blahblahblah1                   0208blahblahblah           0115blahblahblah
                                         0800blahblahblah
                                         0900blahblahblah

Where the AccountID = 1307

Thanks again.
           
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 35158287
Try this:

SELECT D.DDI, C.ConfigDescription, C.ConfigDefault
FROM tDDIs D
LEFT OUTER JOIN tConfigurations C ON C.ID = D.ConfigurationID
WHERE D.AccountID = 1307

Open in new window

0
 

Author Closing Comment

by:Lilliman
ID: 35158317
Fantastic, thank you very much!
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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

650 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