0

问题

  • 我有两个数据库 user_works 数据库和定价数据库。
  • 我想加入两个表 user_table 这是在 user_works 数据库和rent_table 这是在定价数据库中使用 user_id

到目前为止我所做的

  • 我第一次调用 user_works db。
  • 从 user_table 中获取 id。
  • 然后将 ids 传递给定价数据库中的rent_table

例子:

$user_id = select user_id fron user_table WHERE name=abc

将 $user_id 传递给 dbObject user_works db 并获取用户 ID

$rent = select user_id,rent from rent_table WHERE user_id IN ( $user_ids )

将 $rent 传递给定价数据库的 dbObject 我想在 php 应用程序中执行此操作

有没有更好的方法来做到这一点。定价数据库真的很重吗?比如使用临时表等。

4

1 回答 1

1

使用定价数据库连接中的 dblink

select r.*
from
    dblink(
        'dbname=user_works password=password user=user',
        $$ select user_id from user_table where name = 'abc' $$
    ) as u(user_id int)
    inner join
    rent_table r on r.user_id = u.user_id

您需要以超级用户身份创建 dblink 扩展:

create extension dblink;

或者从远程数据库创建一个本地视图:

create view user_table as
select *
from
    dblink(
        'dbname=user_works password=password user=user',
        $$ select user_id, name from user_table $$
    ) as t(user_id int, name text)
;

现在查询它,因为它是本地的:

select * from user_table where name = 'abc';
于 2013-04-16T12:20:12.130 回答