Sql 2008 syntax help please

I have 3 tables:
AllConstituents
Constituents
Accounts

I need to combine them into one table based on DonorID so that the data can be exported into Excel and sent to a third party. Knowledgeable help will be greatly appreciated.
ICantSeeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Please post structure of tables
ICantSeeAuthor Commented:
Here are a few columns for each table. I know I am the one asking for help but do the columns really matter or can we simply use * and select them all from each table with a where clause using the common column DonorId?

AllConstituents:
DonorID
Name
City
State

Constiuents:
DonorId
address1
address2

Accounts:
DonorID
AccountID
Account

Accounts
Najam UddinCommented:
Well if you want only matching result in all 3 tables

SELECT * 
FROM  AllConstituents 
INNER JOIN Constiuents
       ON AllConstituents.DonorID = Constiuents.DonorID
INNER JOIN Accounts
       ON AllConstituents.DonorID = Accounts.DonorID

Open in new window



And for do column matter, yes they matter, * is not best approach. Also for joins, I can not recommend, inner/left/right join unless I know about relation (1 to 1, 1 to many...), more over it depends on what you are trying to get, like for all accounts irrespective of Constiuents, it will be

SELECT * 
FROM  Accounts 
LEFT JOIN AllConstituents
       ON AllConstituents.DonorID = Accounts.DonorID
INNER JOIN Constiuents
       ON AllConstituents.DonorID = Constiuents.DonorID

Open in new window

Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

Jim HornMicrosoft SQL Server Data DudeCommented:
Explain in great detail how you wish to 'combine' them in Excel.  Same tab but all columns running across, joined on the key column DonorID?  Three tabs for the three tables?
ICantSeeAuthor Commented:
LOL... ok. I surrender.

the All constiuents table has most of the data I need, but is missing an accounts column (found in the accounts table) and BirthDate, Entry date and title from the consituents table.

I want to end up with a table that has all of the needed information in it. Each table has a unique ID column named DonorID

I will then use the export feature to send the table that contains all of the information to excel.
Najam UddinCommented:
TRY
SELECT * 
FROM  AllConstituents 
LEFT JOIN Constiuents
       ON AllConstituents.DonorID = Constiuents.DonorID
LEFT JOIN Accounts
       ON AllConstituents.DonorID = Accounts.DonorID

Open in new window

ICantSeeAuthor Commented:
SELECT *
FROM  AllConstituents
LEFT JOIN Constituents
       ON AllConstituents.DonorID = Constituents.DonorID
LEFT JOIN Accounts
       ON AllConstituents.DonorID = Accounts.DonorID

Game me an error on Accounts.DonorID. It does not exist. My mistake.

I then ran
SELECT *
FROM  AllConstituents
LEFT JOIN Constituents
       ON AllConstituents.DonorID = Constituents.DonorID

This seems to have created a table with the data that I need in it, but how do I save it?
Najam UddinCommented:
What do you mean by how do I save it? You want to save in excel  or in database??
ICantSeeAuthor Commented:
I thought that I had to save it as a table so that I could export it to excel? Is this not true? The end result of this particular project is it needs to go to excel.

It would be helpful for the future to be able to save it either place.
Najam UddinCommented:
If you are running this code in sql server you can right click on header of result and copy then paste in excel. If your are doing it in code you have to write code to get it in excel.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ICantSeeAuthor Commented:
That's amazing. And so simple. Thank you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.