[英]How to join tables in two Firebird databases?
Currently I'm working on a simple library project using Embarcadero C++Builder 10.3 Community Edition, and Firebird and FlameRobin to create databases.目前我正在使用 Embarcadero C++Builder 10.3 社区版和 Firebird 和 FlameRobin 来创建一个简单的库项目来创建数据库。
So far, I need only use simple queries, that were connected to a single database.到目前为止,我只需要使用连接到单个数据库的简单查询。 Therefore, I used
TFDConnection
and TFDPhysFbDriverLink
to connect to a .fdb
file.因此,我使用
TFDConnection
和TFDPhysFbDriverLink
连接到.fdb
文件。 Then, TFDQuery
to create SQL commands and TDataSource
.然后,
TFDQuery
创建 SQL 命令和TDataSource
。 It works great.它工作得很好。
Unfortunately, now I must join two tables.不幸的是,现在我必须加入两个表。 How do I write this command?
这个命令怎么写? I tried this:
我试过这个:
SELECT * FROM users_books
join books on
users_books.id_book = books.id
where users_books
and books
are databases.其中
users_books
和books
是数据库。
I got an error:我收到一个错误:
SQL error code = -204
Table unknown
BOOKS.
So I think I must connect somehow to these two databases simultaneously.所以我认为我必须以某种方式同时连接到这两个数据库。 How to do that?
怎么做?
Firebird databases are isolated and don't know about other databases. Firebird 数据库是孤立的,不知道其他数据库。 As a result, it is not possible to join tables across databases with a normal
select
statement.因此,无法使用正常的
select
语句跨数据库连接表。
What you can do, is use PSQL (Procedural SQL), for example in an EXECUTE BLOCK
.您可以做的是使用 PSQL(过程 SQL),例如在
EXECUTE BLOCK
中。 You can then use FOR EXECUTE STATEMENT... ON EXTERNAL
to loop over the table in the other database, and then 'manually' join the local table using FOR SELECT
(or vice versa).然后,您可以使用
FOR EXECUTE STATEMENT... ON EXTERNAL
来循环另一个数据库中的表,然后使用FOR SELECT
(反之亦然)“手动”连接本地表。
For example (assuming a table user_books
in the remote database, and a table books
in the current database):例如(假设远程数据库中有一个表
user_books
,当前数据库中有一个表books
):
execute block
returns (book_id integer, book_title varchar(100), username varchar(50))
as
begin
for execute statement 'select book_id, username from user_books'
on external 'users_books' /* may need AS USER and PASSWORD clause as well */
into book_id, username do
begin
for select book_title from books where id = :book_id
into book_title do
begin
suspend;
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.