Wednesday, March 21, 2012

how can i merge both columns?

I have a column called email_address in table: mailing_list and another column called member_email in table: members and I want to do a SELECt to list the result of both columns in a new column

Create a UNION between the two tables.

Something like:

SELECT ml.eMail_Address

FROM Mailing_List ml

UNION

SELECT m.Member_EMail

FROM Members m

|||

what would be the output column if I use SqlDataReader?

also how can I get the select count(*) of it?

|||

You 'could' just execute the query and see what you get.

As written, there will be one column, named: eMail_Address

SELECT @.@.ROWCOUNT immediately after the query should proved the count.

|||

You can also do:

select *

from (

SELECT ml.eMail_Address

FROM Mailing_List ml

UNION

SELECT m.Member_EMail

FROM Members m ) as emailAddresses

Then you can count them, or do whatever with them, treating this derived table as any other. The first name in the UNION operator will be the name of the column in the result set.

The one thing I wanted to note is that you should always name things the same sort of way. If it is email_address in one place, it should be email_address in another. member_email could mean something else without context and knowledge of the data, while email_address is quite clear.

Naming it member_email_address would be acceptable, as an extension to explain whose email address it is.

|||

Louis,

I agree wholy with your remarks about consistency in naming. Any column containing an email address 'should' be names 'eMailAddress'. As you indicate, anything else is opening the door for confusion.

But it seems redundent to go with your alternative suggestion, member_email_address, since the column would be known as Members.eMailAddress and therefore wouldn't be confused with MailingList.eMailAddress. (I also don't believe in unnecessary underscores separating words either.) I guess I put adding a table name prefix to a column name right up there in the same category as prefixing table names with 'tbl'. Totally wasted keystrokes that offer no added value.

|||

Yeah, I agree with you. That was just an out of context naming suggestion, meaning that the name member_email_address is an acceptable name. I agree wholeheartedly that you would name it email address in the member table, if it was directly referencing the member. But if there was an email that was specifically a email address that was specific to their membership, and it wasn't clear from the name email_address, then member_email_address is a good name.

To me, naming is such a monumental pain, mostly because it is one of the most important steps in the process. A funky name causes a ripple effect of confusion to future users/programmers/support persons/etc. Thanks for clearing that one up!

sql

No comments:

Post a Comment