Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

920 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