加入来自两个不同数据库的3个表?
数据库1(2个表):sandbox_maesc4加入来自两个不同数据库的3个表?
表1:坐标
+----------------------------------------+
|coord_id | section_name | station_number|
+----------------------------------------+
| 1 | A | A7 |
| 2 | B | B20 |
| 3 | C | C3 |
| 4 | D | D14 |
| 5 | E | E9 |
+----------------------------------------+
表2:workstation_userlogged
+----------------------------------+
| id | ws | user |
+----------------------------------+
| 1 | COMP123 | ryan |
| 2 | COMP345 | luda |
| 3 | COMP567 | chris |
| 4 | COMP891 | michel|
| 5 | COMP444 | isabel|
+----------------------------------+
数据库2(1个表):softphone_materials_updater
表1 :工作站
+----------------------------+
| ID | ws | pod |
+----------------------------+
| 1 | COMP123 | A07 |
| 2 | COMP345 | B20 |
| 3 | COMP567 | C03 |
| 4 | COMP891 | D14 |
| 5 | COMP444 | E09 |
+----------------------------+
问题:在数据库
我只有读访问2. 所以我做了这两个领域SELECT查询并创建表“userlogged”。
现在,我想通过将表“工作站” 与它们的“station_number”字段与“pod”字段的关系连接起来,将“坐标”和“userlogged”两个表结合起来。我怎样才能做到这一点?以下是我尝试过的查询,但不起作用。
我在“坐标”表(X,Y字段与实际坐标)中有额外的字段。在PHP中,我使用所有字段在屏幕上显示它们。
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
我想也许这就是你想要的?
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
不确定数据库和表名称,他们似乎不同之间的示例查询和说明。
我更新了名称和全部。仍然你给我的查询不起作用。坐标表有“x_coord”和“y_coord”字段,这些字段在图中没有包含。再次感谢 – mario 2014-10-27 16:16:24
当你说它不起作用有什么问题?你是否收到错误或者你只是没有得到正确的结果?在你的表描述中,似乎没有任何_username_和_hostname_列,也许它应该是_wsu.user作为username_和_wsu.ws作为hostname_? – jpw 2014-10-27 16:20:43
实际上我只是让它工作,在w.pod之后还有一个额外的“,”。我只是修改了查询的内容。非常感谢! – mario 2014-10-27 16:24:32
究竟没有工作? – pancho018 2014-10-27 16:01:40
该查询不选择我想要显示的字段 – mario 2014-10-27 16:16:59