连接来自不同数据库的两个mysql表 [英] Connecting two mysql tables from different databases

查看:204
本文介绍了连接来自不同数据库的两个mysql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


美好的一天...


我只想问...你怎么连接两张桌子2个不同的数据库使用PHP脚本?因为,包含客户表的一个数据库专用于一个项目,然后是另一个也使用客户表的项目...客户端不想重新输入从第一个数据库到第二个数据库的所有值。转储不是一个选项,因为他们希望这两个项目在不同的数据库中运行......每当用户从第一个数据库更改customer表时,它也应该影响第二个数据库中的customer表...



我现在需要意见......我刚刚开始使用php和mysql ..



谢谢:)

解决方案

我不知道这是否是最好的方法,但是我会研究开发两个单独的类,一个是使用每个数据库。这样,只要您需要更新数据,您就可以将数据发送到每个类中,每个类都会更新相应的数据库。我知道你会复制一些代码,这种方法会破坏类的目的,但是再一次,客户已经在复制数据库中的数据,并且不想移动到单个数据库。

加入单独的数据库并不是很困难(假设它们位于同一台服务器上)就像你使用table.field指定字段一样,你也可以使用数据库 .table.field"下面是两个数据库连接的示例:


[PHP]


sql =" SELECT db1.table1.somefield,db2.table1 .somefield FROM db1.table1 INNER JOIN db2.table1 ON db1.table1.someid = db2.table1.someid WHERE db1.table1.somefield =''queryCrit'';" [/ PHP]


您只需像在一个数据库中工作一样编写查询,只需使用点符号指定数据库。


Greg


Hello,

Good day to all...

I just want to ask.. How would you connect two tables from 2 different databases using PHP scripts? Because, one database that contains customer table is dedicated to one project, and then here comes another project that also uses the customer table...the clients dont want to reenter all of values from the first database to the second database. Dumping would not be an option cause they want the two projects to run in different databases... and whenever the user made changes to the customer table from the first database, it should also affect the customer table from the second database...


I need opinions now... I''m just starting to use php and mysql..


Thanks :)

解决方案

I don''t know if this would be the best way to do it, but I would look into developing two separate classes, one to work with each database. That way, anytime you would need to update the data, you could just send the data into each of the classes and each class would update the corresponding database. I know that you would be duplicating some code, and that kind of defeats the purpose of the class, but then again, the customer already is duplicating data in the databases and doesn''t want to move to a single database.


It''s really not difficult to join seperate databases (assuming they reside on the same server) Just like you would specify fields using the "table.field", you can also use "database.table.field" Below is an example of a two database join:

[PHP]


sql="SELECT db1.table1.somefield, db2.table1.somefield FROM db1.table1 INNER JOIN db2.table1 ON db1.table1.someid = db2.table1.someid WHERE db1.table1.somefield = ''queryCrit'';"[/PHP]

You simply write you query just like you would if you were working in one db, just use the dot notation to specify your databases as well.

Greg


这篇关于连接来自不同数据库的两个mysql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆