Off Topic - SQL Help

All -

I realize this is off topic but I’m hoping someone might be able to point me in the right direction.

I’ve got a background agent connecting to a SQL server using ADODB.

So far everything is working but I’m looking for help in optimizing a SQL query (I need to send an email to all valid users).

In the SQL database I’ve got two tables:

Table A: List of valid users, primary key is the user’s network login id.

user_id


NT124

NT126

.

.

Table B: list of ALL users, primary key is network login id and additional column is their email address.

user_id email_address


NT123 user1@email.com

NT124 user2@email.com

NT125 user3@email.com

NT126 user4@email.com

.

.

I know that I can execute two queries that will get the job done (Query table B, then look up user in Table A) but I imagine their must be a way to execute only one query against both tables.

Thanks in advance…

Ernest

Subject: Off Topic - SQL Help

Ernest,

I’m no SQL expert, but try something like the following.

SELECT b.email_address

FROM TableA a, TableB b

WHERE a.user_id = b.user_id

This creates a join, where the user_id’s are the same. The a. / b. provide an alias to your table names allowing you to select one or the other appropriately when the columns have the same name.

Use DISTINCT in after Select if there is a possibility of having duplicate email addresses and you need to get rid of them.

Hope this helps.

John

Subject: RE: Off Topic - SQL Help

Thanks John - I really appreciate this…

Subject: RE: Off Topic - SQL Help