有三张表,分别如下:
select * from vehicle
select * from station
select * from vehicle_station
需求:
vehicle和station表示多对多的关系,需要把vehicle表对应的station表的第二字段查出来放到一个字段,如果对应多条,用逗号隔开放到一个字段。
解决方案:
SELECT v.*, array_to_string(ARRAY (SELECT station_name FROM station WHERE ID IN (SELECT station_id FROM vehicle_station WHERE vehicle_id = v. ID)),’,’) station_names FROM vehicle v
结果如下:
postgres array_to_string和array的用法实例
最新推荐文章于 2024-07-22 03:15:37 发布