可以请帮助我,我正在尝试解决这个问题很长一段时间......我有表格产品和相关产品(顶级产品由其他基础产品组成)。目标:我想获得所有基础产品。所以,表看起来像:
product_id related_product_ids
------------------------------------------------
1143 1213
1255 1245
1261 1229,1239,1309,1237,1305,1243,1143
我通过查询得到了这个:
select max(p.id) as product_id, array_to_string(array_agg(p2p.related_product_id), ',') as related_product_ids
from product p
left join product_to_product p2p on p2p.product_id = p.id
where p.id in (select product_id from order_line where wo_id = 262834)
group by p.id, p2p.product_id
我想related_product_ids
输入产品表以获取所有相关产品。所以,实际上我通过运行从所有必要的值中创建了数组
select array_agg(p2p.related_product_id) as id
from product p
left join product_to_product p2p on p2p.product_id = p.id
where p.id in (select product_id from order_line where wo_id = 262834)
related_product_ids
---------------------------------------------
{1309,1143,1229,1239,1243,1237,1305,1245,1213}
我尝试了以下操作,但没有成功:
select *
from product
where id = ANY(select array_agg(p2p.related_product_id) as id
from product p
left join product_to_product p2p on p2p.product_id = p.id
where p.id in (select product_id from order_line where wo_id = 262834))
Error: ERROR: operator does not exist: integer = integer[] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 39, SQLState: 42883, ErrorCode: 0
或以下:
select *
from product
where id in (select array_to_string(array_agg(p2p.related_product_id), ',') as id
from product p
left join product_to_product p2p on p2p.product_id = p.id
where p.id in (select product_id from order_line where wo_id = 262834))
Error: ERROR: operator does not exist: integer = integer[] Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts. Position: 36, SQLState: 42883, ErrorCode: 0
和许多其他尝试
所以最后我需要的是
select *
from product
where id in (1309,1143,1229,1239,1243,1237,1305,1245,1213)
(来自 的值related_product_ids
)
如何将整数数组(related_product_ids)转换为值....或者您可以建议不同的更好方法?