我在管理这样的查询时遇到了困难:
SELECT c.id, p.id FROM claim c, procedure p LIMIT 1;
此查询将返回以下设置:
id | id
----+----
49 | 1
有没有办法让它为列名返回c.id
和p.id
?这个不起作用:
SELECT c.id as c.id, p.id as c.id FROM claim c, procedure p LIMIT 1;
或者这是我的最终解决方案吗?
SELECT c.id as c_id, p.id as p_id FROM claim c, procedure p LIMIT 1;
答案 0 :(得分:3)
SELECT c.id AS "c.id", p.id AS "p.id" FROM claim c, procedure p LIMIT 1;
或简单地说:
SELECT c.id "c.id", p.id "p.id" FROM claim c, procedure p LIMIT 1;
答案 1 :(得分:2)
使用正确的命名约定,您很少需要开始处理此问题。
我建议改用这样的东西:
CREATE TABLE claim (claim_id serial PRIMARY KEY, ...);
CREATE TABLE procedure (procedure_id serial PRIMARY KEY, ...);
对于列名, "id"
是非常糟糕的选择。不幸的是,一些半机智的ORM使用这种反模式。尽量避免使用它。
相关:
Join one to many and retrieve single result
Using UNNEST with a JOIN
答案 2 :(得分:1)
最后一个我认为是最正确的答案是使用方括号[]
SELECT c.id AS [c.id], p.id AS [p.id]
FROM claim c, procedure p LIMIT 1;