Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL statement issue

Status
Not open for further replies.

LucyP

Programmer
Jan 17, 2001
51
ES
I have two related tables in a database. One stores the names and descriptions of various email lists people can subscribe to, and the other stores the email addresses. So that the system is fully flexible, there is a separate row in the second table for every email address (containing listname and emailaddress).

What I need is a sql statement that will return all of the values from the listnames table, along with whether or not a particular email address is listed as belonging to that list. At present I'm having to do this with 2 sql staements:

SELECT listname FROM listnames

and

SELECT listname FROM emailaddresses WHERE emailaddress='test@test.com'

I'm then using a few lines of VB to marry the matching items up so that the html page can display a list of all the email lists with 'subscribed'/'not subscribed'

There must be an easier way to do it! (eg can I specify 'distinct' just for one item, rather than for whole rows?)
 

Use a LEFT JOIN query. In this case, join the listnamse table to a query that selects record from EmailAddresses based for a particular EmailAddress.

Select
l.Listname,
Case
When e.EMailAddress is null Then 'Not enrolled'
Else 'Enrolled'
End As EnrollStatus
From listnames As l
Left Join
(Select listname, emailaddress
From emailaddresses
Where EmailAddress='bob@email.com') As e
On l.listname=e.listname Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top