mysql中同时查询两个数据库中的数据

mysql中同时查询两个数据库中的数据,第1张

1、打开php的编辑器sublime,新建一个文件,写上注释内容。

2、新建一个函数chaxun。

3、连接数据库,填写数据库的用户名,密码,主机名以及要使用的数据库。

4、填写查询的sql语句。select * from test1。

5、读取查询到的数据,我们这里用到的函数是fetch_assoc来实现。

6、调用这个函数。

7、打开本地的服务器,输入网址进行访问测试。

1. 子查询方法

select * 

from DB2.table2 

where 字段 in (select table1中相应字段 from DB1.table1 where table1中相应字段=相应值)

 

2. 左连接方法

select table2.* 

from DB2.table2 left join DB1.table1 

on table1.字段 = table2.相应字段 

where table2.相应字段 = 相应值

 

2. 交叉连接方法

select table2.* 

from DB2.table2, DB1.table1 

where table1.字段 = table2.相应字段 and table2.相应字段 = 相应值

1: $sql = show databases like 'gunsoul%' //查出所有的库名

2 循环库名拼接SQL语句

$sql2 =''

foreach($databases as $db){

$sql2 .="select * from $db.table union"

}

3 执行sql2打印结果


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/7479968.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-06
下一篇2023-04-06

发表评论

登录后才能评论

评论列表(0条)

    保存