PostgreSQL:SQL 中的硬编码字符串

Len*_*nie 2 postgresql

如何在 PostgreSQL SELECT 中有硬编码字符串?

就像是:

SELECT
 'some value' As FieldName
 FROM table
Run Code Online (Sandbox Code Playgroud)

上面给出了 pgAdmin 中的错误。

小智 7

Craig Ringers 的回答(一如既往)是正确的,我想为其添加以下扩展名:

--
-- This is how you can define a hardcoded value as column (here as the type 'text').
-- Instead of the explicit 'CAST(...)' call it's also possible to use the
-- '::text' short version.
--
-- NOTE-1: The short version or omitting the type completely might not
--         work in every SQL client (e. g. SquirrelSQL), while the
--         CAST(...) call is pretty safe.
--
-- NOTE-2: Please always use lower case column names!
--
SELECT CAST('some value' AS text) AS fieldname;
Run Code Online (Sandbox Code Playgroud)


Jou*_*uge -3

在 SELECT 关键字之后只能插入列名。也许你正在寻找这样的东西

SELECT 'columnname' AS 'alias'
FROM 'tablename'
WHERE 'alias/columnname' = 'hardcodedvalue';
Run Code Online (Sandbox Code Playgroud)