C# and SQL

  • Thread starter Thread starter Maziar Aflatoun
  • Start date Start date
M

Maziar Aflatoun

Hi everyone,

I have 2 tables with table A containing an 'email' field and table B
containing 2 fields 'primaryemail' and 'secondaryemail'. Now is it possible
to issue a query that would return only the unqiue email addresses in these
3 fields? So a long list with no duplicate emails(distinct).

Thank you
Maz.
 
You might be better off asking this in a SQL group, but I think you could do
a union of selects, one select for each field. You would then need to get a
distinct listing of the whole. I'm not sure, off the top of my head exactly
how you'd go about that. It may require a temporary table where you insert
based on the union and then select distinct on the temporary table.

But like I said, the SQL newsgroups may offer more specific answers.

Pete
 
yes, just query the Table A, since emails in Table A is unqiue. and should
have primary key to identify them
 
Hi everyone,

I have 2 tables with table A containing an 'email' field and table B
containing 2 fields 'primaryemail' and 'secondaryemail'. Now is it possible
to issue a query that would return only the unqiue email addresses in these
3 fields? So a long list with no duplicate emails(distinct).

Thank you
Maz.

Not sure, but it sounds like you want more than you are asking for
here. I read into your question that table A will have either a
Primary or Secondary email address. If this is the case, then Table A
will have everything you need.

Can you show some sample data and what you want out of your query?
 
...
You might be better off asking this in a SQL group,

I agree, as it's not connected to C#...
but I think you could do a union of selects,
one select for each field. You would then need
to get a distinct listing of the whole.

It should just be necessary to do three separate queries, as the "union" in
itself *should* remove duplicates:

select email from tableA
UNION
select primaryemail from tableB
UNION
select secondaryemail from tableB

However, there's no guarantee for this to work in the OP's specific case as
he didn't mention which database the tables resides in, and not all
databases conform to the SQL-standard. E.g. in some databases it's necessary
to use three distinct queries:

select distinct email from tableA
UNION
select distinct primaryemail from tableB
UNION
select distinct secondaryemail from tableB

....and in other databases, the union operation doesn't even exist...

// Bjorn A
 
Well,

Imagine the following

In Table A, I have a field 'email'
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

and in Table B, I have the 2 fields 'primaryemail' and 'secondaryemail' with
the following data

Under field 'primaryemail' I have
(e-mail address removed)
(e-mail address removed)
(e-mail address removed)

and finally in the field 'secondaryemail' I have
(e-mail address removed)
(e-mail address removed)

Now how would I get a list containing only the unique email address of all
the emails above?

Thank you
Maz.
 
Caution: AIR CODE follows...

Select Email from TblA
UNION
Select PrimaryEmail from TblB as Email
UNION
Select SecondaryEmail from TblB as Email
 
Back
Top