如何 select 只为 sql 中的函数返回没有行 ID 和列名的结果值?

How to select just result value without row id and column name for function in sql?

我执行 pgRouting,我需要从我的 SELECT.

向 dijkstra 算法插入整数结果
SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
  FROM pgr_dijkstra(
'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
34, 3000, false
  ) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id ;

这是可行的,但是如果我想用这个查询将 dijkstra 函数中的节点 34 替换为我所在街道的节点号:

SELECT pl.source::integer 
FROM planet_osm_roads pl 
WHERE pl.name LIKE ''street_name'' 
LIMIT 1

一起:

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
  FROM pgr_dijkstra(
'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
'SELECT pl.source::integer FROM planet_osm_roads pl WHERE pl.name LIKE ''street_name'' LIMIT 1',
 3000, false
  ) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id ;

它将失败并出现错误:

ERROR:  function pgr_dijkstra(unknown, unknown, integer, boolean) is not unique
LINE 93:   FROM pgr_dijkstra(
                ^
HINT:  Could not choose a best candidate function. You might need to add explicit type casts.

我认为,这是因为我的 select 查询返回 sql 结果以及行 ID 和列名。但也许还有另一个问题。

如何只输出一个整数?

不要将查询作为字符串传递:

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
FROM pgr_dijkstra(
  'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
  (SELECT pl.source::integer FROM planet_osm_roads pl WHERE pl.name LIKE 'street_name' LIMIT 1),
   3000, false
) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id;

或者使用派生的 table

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
FROM (
  SELECT pl.source::integer as source
  FROM planet_osm_roads pl 
  WHERE pl.name 
  LIKE 'street_name' LIMIT 1
) pl 
  join lateral pgr_dijkstra(
    'SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
    pl.source,
    3000, false
  ) as di on true
  JOIN planet_osm_roads pt ON di.edge = pt.osm_id;

Vertex 是整数参数,您正试图将文本放在那里。 只需将 ' 更改为 ()

SELECT ST_AsGeoJSON(ST_Transform(way, 4326)) AS geometry
  FROM pgr_dijkstra('SELECT osm_id AS id, source, target, st_length(way) as cost FROM planet_osm_roads',
(SELECT pl.source::integer FROM planet_osm_roads pl WHERE pl.name LIKE ''street_name'' LIMIT 1),
 3000, false
  ) as di
  JOIN planet_osm_roads pt
  ON di.edge = pt.osm_id ;

这是一种方法,另一种方法是编写函数,将街道名称作为参数并将您的 geojson 作为输出。