Solved

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

Posted on 2011-03-17
4
195 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 500 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to shrink a transaction log file down to a reasonable size.

739 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