如何在我的sql中从另一个数据库中选择表的数据?

时间:2015-01-12 06:56:46

标签: mysql sql database

我有两个数据库db1和db2, 如何在db2中从db1 tb1中选择数据?

我尝试使用sql如下,但这是错误的

select db1.* 
from db1.table1 dt1,db1.table2 dt2
where dt1.table1.id = dt2.table2.id

请帮忙。

由于

2 个答案:

答案 0 :(得分:1)

您正在直接查询数据库。将表名放在select子句db1.table1.*中。

并且您在where子句 where dt1.table1.id = dt2.table2.id

中使用表的别名

尝试代替dt1 = db1 dt2 = db2

select db1.table1.* 
from db1.table1 dt1,db1.table2 dt2
where db1.table1.id = db2.table2.id

答案 1 :(得分:0)

您已经在from子句中对表进行了别名,您还需要在查询的其他位置使用相同的别名。

select dt1.* 
from db1.table1 dt1,db1.table2 dt2
where dt1.id = dt2.id