首页 > 解决方案 > 使用 3 个表连接获取 API 列表

问题描述

我有 3 个表,我想从中获取一些数据:

主表:

    create table if not exists exchange
    (
        id bigint,
        name varchar(255),
    );
    
    INSERT INTO exchange (id, name) 
    VALUES (1, 'PARIS');

第二张表:

    create table exchange_api
    (
        id                           bigint,
        api_name                     varchar(255),
        exchange_id                  bigint
    );

INSERT INTO exchange_api (id, api_name, exchange_id)
   VALUES (8, 'PARIS-API', 5);

第三张表:

create table instrument
(
    id               bigint,
    symbol           varchar(255),
    exchange_api_id  bigint
);

INSERT INTO instrument (id, symbol, exchange_api_id)
VALUES (47, 'PARIS-ARD', 8);
INSERT INTO instrument (id, symbol, exchange_api_id)
VALUES (48, 'PARIS-SRD', 8);

我需要按照以下方式映射表格:

exchange我要发送参数的第一张桌子PARIS

结果我想得到这个结果:

PARIS-ARD
PARIS-SRD

我怎样才能使用这些数据JOIN

标签: sqlpostgresql

解决方案


Select I.symbol from instrument I 
inner join exchange_api API 
On I.exchange_api_id API.id
Inner JOIN exchange E
ON API.exchange_id = E.id
where E.name = 'PARIS'

如果您想将 Pass 'PARIS'作为值传递,那么它将返回

PARIS-ARD
PARIS-SRD

作为输出


推荐阅读