两张表在不同的数据库,如何关联查询?

两张表在不同的数据库,如何关联查询?,第1张

mysql支持多个库中不同表的关联查询,你可以随便链接一个数据库

然后,sql语句为:

select * from db1.table1 left join db2.table2 on db1.table1.id = db2.table2.id

只要用数据库名加上"."就能调用相应数据库的数据表了.

数据库名.表名

扩展资料

mysql查询语句

1、查询一张表:     select * from 表名;

2、查询指定字段:select 字段1,字段2,字段3....from 表名;

3、where条件查询:select 字段1,字段2,字段3 frome 表名 where 条件表达式;

例:select * from t_studect where id=1

  select * from t_student where age>22

4、带in关键字查询:select 字段1,字段2 frome 表名 where 字段 [not]in(元素1,元素2);

例:select * from t_student where age in (21,23)     

   select * from t_student where age not in (21,23)

5、带between and的范围查询:select 字段1,字段2 frome 表名 where 字段 [not]between 取值1 and 取值2;

例:select * frome t_student where age between 21 and 29

     select * frome t_student where age not between 21 and 29

create public batabase link a_to_b\x0d\x0aconnect to (被 *** 作表所在库的用户名) identified by (被 *** 作表所在库的密码)\x0d\x0ausing ‘(被 *** 作表所属实例名)’\x0d\x0a然后在 *** 作实例中进行测试:\x0d\x0aselect 1 from dual@a_to_b\x0d\x0a如果有正确结果 那么你就建立了一个dblink了。\x0d\x0a \x0d\x0a这个时候就可以 跨库查询了。

1、首先就是创建几个没有任何关系的表,但是注意,你在将要作为外键表的表上必须使用与将要作为主键表的数据类型相同。

2、将能唯一标识的那一行设置为主键,其他表类似。

3、接下来添加关系,如下图。

4、拖动需要添加的关系,直接看图。

5、关联完成,如图。


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

原文地址: http://outofmemory.cn/sjk/6632839.html

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

发表评论

登录后才能评论

评论列表(0条)

保存