[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

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

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
Lilliman
Asked:
Lilliman
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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
 
LillimanAuthor Commented:
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
 
wdosanjosCommented:
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
 
LillimanAuthor Commented:
Fantastic, thank you very much!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now