duanchun5520 2014-10-21 13:49 采纳率: 0%
浏览 53
已采纳

如何从不同的数据库中连接两个表? [关闭]

Database 1 : sandbox

table 1 : coordinates

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

database 2 : phone

table 2 : workstations

I only have READ privilege

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

PROBLEM:

I only have READ privilege on that second table of that database.

I want to join both tables so I can display the "ws" field on screen for my PHP script. My "station_number" field values are written differently from the "pod" field (they have a zero in front of the letter if it is a single digit after the letter) does it make a difference?

I've seen examples online on how to join but for some reason I can't get it to work.

Do I need to create an extra field on my main Table to store the field values from "ws" or what? I'm a bit confused.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dougang1605 2014-10-21 13:54
    关注

    Yes, it makes a difference if there's a 0 in one and not in the other. You'll have to modify the value of one or the other. Something like SUBSTR(station_number, 0, 1) + LPAD(SUBSTR(station_number, 2), 2, '0').

    And to join the tables from different databases, you just have to put the database name in front of the table name. Probably something like sandbox.coordinates.

    So your query might look something like this:

    SELECT
        *
    FROM
        sandbox.coordinates c
        INNER JOIN phone.workstations w
            ON (SUBSTR(c.station_number, 0, 1) + LPAD(SUBSTR(c.station_number, 2), 2, '0')) = w.pod
    

    If you can update one of your tables to make the columns match, then your query is as simple as:

    SELECT
        *
    FROM
        sandbox.coordinates c
        INNER JOIN phone.workstations w
            ON c.station_number = w.pod
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条