Jump to content

Select results from 2 tables in different databases on same server?


localtechguy

Recommended Posts

Hello,

 

I am trying to grab the results of two identical tables (in terms of format and fields etc..) from two different databases located on the same server/host.

 

example TableA on DatabaseA:

Select firstname, lastname From TableA WHERE date= $today

 

 

results would be : John, Smith

 

example2 TableA on DatabaseB :

Select firstname, lastname From TableA WHERE date= $today

 

results for this : Chuck, Norris

 

I need a single select statement to give me the results :

John, Smith

Chuck, Norris

 

Does anyone know how i can accomplish this?

 

Thank you in advance for your help as I'v been struggling with this for awhile now and i can only seem to find documentation on JOINS which Im pretty sure is not what i need in this situation.

Link to comment
Share on other sites

Thanks for the quick reply!

 

SELECT DatabaseA.TableA.firstname as firstnameA, DatabaseB.TableA.firstname as firstnameB, DatabaseA.TableA.lastname as lastnameA, DatabaseB.TableA.lastname as lastnameB FROM DatabaseA.TableA, DatabaseB.TableA WHERE DatabaseA.TableA.dateA = $today AND DatabaseB.TableA.dateA = $today;

 

After taking a look at the link you relayed, it seems that the query they are suggesting would give me the results

 

firsnameA

firstnameB

lastnameA

lastnameB

John

Chuck

Smith

Norris

 

I need the results to be appended not as new columns but at the end of the data set of one table using the same columns

 

firstname

lastname

John

Smith

Chuck

Norris

 

It's looking like the only way to do this is maybe putting together a temporary table on the fly? Any advise?

 

side note: as a further explanation of why i need the query to behave this way, is because i have a php script that dumps the result of a query into a .csv file (spreadsheet) so doing what was suggested above or doing a JOIN would give me the data, but it will spit it out to the spreadsheet by appending new columns as shown above instead of placing the data at the end of the data of the first table. ugh... im confusing myself.  :shrug:

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.