Solved

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

Posted on 2011-03-17
4
190 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
  • 2
4 Comments
 
LVL 142

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now