gpt4 book ai didi

postgresql - 添加列但保留特定的 ID

转载 作者:行者123 更新时间:2023-11-29 12:39:57 25 4
gpt4 key购买 nike

我有一个“列表”表,如下所示:

| listing_id |                   amenities                      |
|------------|--------------------------------------------------|
| 5629709 | {"Air conditioning",Heating, Essentials,Shampoo} |
| 4156372 | {"Wireless Internet",Kitchen,"Pets allowed"} |

还有另一个像这样的“Amenity”表:

| amenity_id |                     amenities                    |
|------------|--------------------------------------------------|
| 1 | Air conditioning |
| 2 | Kitchen |
| 3 | Heating |

有没有办法像这样将两个表连接到一个新的“Listing_Amenity”中:

| listing_id | amenities |
|------------|-----------|
| 5629709 | 1 |
| 5629709 | 3 |
| 4156372 | 2 |

最佳答案

你可以使用 unnest:

CREATE TABLE Listing_Amenity
AS
SELECT l.listing_id, a.amenity_id
FROM Listing l
, unnest(l.ammenities) sub(elem)
JOIN Amenity a
ON a.ammenities = sub.elem;

db<>fiddle demo

关于postgresql - 添加列但保留特定的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55563682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com