0
SELECT  concat(first_name,last_name)  AS full_name from customer
ORDER BY  length(full_name);

我试图在 postgre sql 数据库中运行它。我给我这个错误

[42703] ERROR: column "full_name" does not exist

我将如何解决这个问题?按全名长度对行进行排序。

4

3 回答 3

0

您可以尝试以下任何一种方法吗?

SELECT  concat(first_name,last_name) COLLATE "C"  AS full_name from customer
ORDER BY  length(full_name)

或者

SELECT  concat(first_name,last_name)  from customer
ORDER BY  length(concat(first_name,last_name))
于 2020-08-08T03:09:22.553 回答
0

这将起作用,并避免双重使用concat()

WITH results AS (
  SELECT concat(first_name, last_name) AS full_name FROM customer
)
SELECT full_name FROM results ORDER BY length(full_name)
于 2020-08-08T03:14:59.140 回答
0

order byPostgres 通过允许列别名作为键来遵守标准。所以这有效:

SELECT  CONCAT(first_name, last_name)  AS full_name
FROM customer
ORDER BY full_name;

但是,它不会将此扩展到使用别名的表达式。您可以使用子查询或 CTE 解决此问题。我还可以建议横向连接:

SELECT v.full_name
FROM customer c CROSS JOIN LATERAL
     (VALUES (CONCAT(c.first_name, c.last_name))) v(full_name)
ORDER BY v.full_name;
于 2020-08-08T12:52:57.923 回答