postgresql 如何在约束参数显示相同结果时获得查询的完整输出

ohtdti5x  于 2023-02-04  发布在  PostgreSQL
关注(0)|答案(1)|浏览(113)

我有以下疑问:

select  names as "names"  from tbl1 where id_name  in
                    (select owner_id from tbl2 where id in
                    (select  product_id FROM tbl3 where tbl3.id IN (10, 11, 15)))

我得到的是响应,因为id10和11得到的是相同的名称Ford

names
Ford
Fiat

我想做的是:
1.得到两行作为响应,即使它们是相同的:

names
  Ford
  Ford
  Fiat

1.我想在结果中得到www.example.com,如下所示: tbl3.id in the result, like this:

id    names
10    Ford
11    Ford
15    Fiat
xoefb8l8

xoefb8l81#

如@IMSoP所述,您需要JOIN您的表。请尝试以下操作:

SELECT t3.id, t1.names
  FROM tbl1 AS t1
 INNER JOIN tbl2 AS t2
    ON t1.id_name = t2.owner_id
 INNER JOINO tbl3 AS t3
    ON t2.id = t3.product_id
 WHERE t3.id IN (10, 11, 15)

相关问题