0

I have a two tables TabA and TabB. TabA is in DB_A and TabB in 'DBX'_B.

Now I need to get the DBX_B name from a field from TabA of DBA and join them in a query to pull the data from both these tables.

 DBA.TabA:
 ID        DB_Name      UserName    Password
 -------------------------------------------
 101       DBX          xyz         abc


 DBX_B.TabB:

 ID        Type      FirstName      LastName
 -------------------------------------------
 101       Admin     xyz            abc

I need to pull ID, Username, Password from DBA.TabA and pull Type, Firstname, LastName from DBX_B.TabB. But the 2nd database name to use can be identified from DB_NAME and concat it with string like _B'. So the 2nd database to pull from isDBA.TabA.DB_Name' + _B. Join these two tables on ID from both.

The query can look something like:

  SELECT DBA.TabA.ID, DBA.TabA.Username, DBA.TabA.Password, 
         DB2.TabB.Type, DB2.TabB.FirstName, DB2.TabB.Lastname 
    FROM DBA, CONCAT(DBA.TabA.DB_Name, '_B') as DB2
   WHERE DBA.TabA.ID = DB2.TabB.ID

Of course, we can use Join too instead of where.

Is something like this possible? Ideas?

4

2 に答える 2

0

As Oli said you can't do this in mysql and most relational database programs, you have to run a use command to instruct the RDBMS to use your database cause it could have many different databases stored in it.

For example when if you log onto mysql you have to run:

use DB_NAME;

to instruct it to use that particular database then only you can access its tables.

于 2012-07-13T17:10:35.880 に答える
0

You could possibly do this with some subSelects, but performance will take a huge hit, to the point that it just won't be worth it. You might be better off pulling your tables into whatever your middle tier is (php?) and joining the data together in there. How you do that will depend on what your middle tier is.

There is something flawed in your database design, if you are still in the design phase, STOP, and redesign, hire someone if you have to, getting your Data model designed correctly now will save you so much future pain. If you are maintaining something and stuck with what you have, using the middle tier will be your best bet.

于 2012-07-13T17:13:17.740 に答える