简体   繁体   中英

Joining 3 tables from two different Databases?

Database 1 (2 tables) : sandbox_maesc4

table 1: coordinates

+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
|   1     |    A         |  A7           |
|   2     |    B         |  B20          |
|   3     |    C         |  C3           |
|   4     |    D         |  D14          |
|   5     |    E         |  E9           |
+----------------------------------------+

table 2: workstation_userlogged

+----------------------------------+
|  id     |    ws         |  user  |
+----------------------------------+
|   1     |    COMP123    |  ryan  |
|   2     |    COMP345    |  luda  |
|   3     |    COMP567    |  chris |
|   4     |    COMP891    |  michel|
|   5     |    COMP444    |  isabel|
+----------------------------------+

Database 2 (1 table): softphone_materials_updater

Table 1: workstation

+----------------------------+
|   ID  |    ws     |  pod   |
+----------------------------+
|  1    |  COMP123  |  A07   |
|  2    |  COMP345  |  B20   |
|  3    |  COMP567  |  C03   |
|  4    |  COMP891  |  D14   |
|  5    |  COMP444  |  E09   |
+----------------------------+

Problem:

I only have read access in Database 2. So I did a SELECT query for both fields and created the table "userlogged".

Now, I want to combine both tables "coordinates" and "userlogged" by joining table "workstation" with their relation of the "station_number" field and "pod" field from. How can I achieve this? Below is the query that I tried but doesnt work.

I have extra fields in "coordinates" table (X,Y fields with actual coordinates). In PHP I use all fields to show them on screen.

SELECT 
  coordinate_id, 
  section_name, 
  x_coord, 
  y_coord, 
  ws.username, 
  ws.hostname, 
  w.pod, 
FROM 
  sandbox_maesc4.coordinates c, 
  sandbox_maesc4.workstation_userlogged ws
INNER JOIN
  softphone_materials_updater.workstations w
  ON c.station_number = w.pod

I think maybe this is what you want?

SELECT 
  coordinate_id, 
  section_name, 
  x_coord, 
  y_coord, 
  wsu.username, 
  wsu.hostname, 
  w.pod 
FROM 
  sandbox_maesc4.coordinates c  
INNER JOIN
  softphone_materials_updater.workstations w
  ON c.station_number = w.pod
INNER JOIN
  sandbox_maesc4.workstation_userlogged wsu
  ON w.ws = wsu.ws

Not sure about the database and table names, they seem to differ between your sample query and the description.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM