计算列名称' count'返回多行。为什么?

时间:2017-08-18 08:48:26

标签: sql postgresql count postgresql-9.2

我不明白这个问题的原因:

select count(base.*) from mytable base;

会返回多行。

select count(1) from mytable base;

返回正确的计数。

有一个名为count的列。

有人可以解释一下这种行为吗?

以下是架构中的信息:

table_catalog,table_schema,table_name,column_name,ordinal_position,column_default,is_nullable,data_type,character_maximum_length,character_octet_length,numeric_precision,numeric_precision_radix,numeric_scale,datetime_precision,interval_type,interval_precision,character_set_catalog,character_set_schema,character_set_name,collation_catalog,collation_schema,collation_name,domain_catalog,domain_schema,domain_name,udt_catalog,udt_schema,udt_name,scope_catalog,scope_schema,scope_name,maximum_cardinality,dtd_identifier,is_self_referencing,is_identity,identity_generation,identity_start,identity_increment,identity_maximum,identity_minimum,identity_cycle,is_generated,generation_expression,is_updatable
mydatabase,vcs,mytable,controlepunt,1,,YES,text,,1073741824,,,,,,,,,,,,,,,,mydatabase,pg_catalog,text,,,,,1,NO,NO,,,,,,,NEVER,,YES
mydatabase,vcs,mytable,norm,2,,YES,text,,1073741824,,,,,,,,,,,,,,,,mydatabase,pg_catalog,text,,,,,2,NO,NO,,,,,,,NEVER,,YES
mydatabase,vcs,mytable,fout,3,,YES,text,,1073741824,,,,,,,,,,,,,,,,mydatabase,pg_catalog,text,,,,,3,NO,NO,,,,,,,NEVER,,YES
mydatabase,vcs,mytable,count,4,,YES,bigint,,,64,2,0,,,,,,,,,,,,,mydatabase,pg_catalog,int8,,,,,4,NO,NO,,,,,,,NEVER,,YES
mydatabase,vcs,mytable,id,5,,YES,bigint,,,64,2,0,,,,,,,,,,,,,mydatabase,pg_catalog,int8,,,,,5,NO,NO,,,,,,,NEVER,,YES

2 个答案:

答案 0 :(得分:2)

这种风格显然被称为功能符号

它使table.colcol(table)等效。

tabletable.*返回同一组列。

此问题提供了更多相关信息:Using functional notation in PostgreSQL queries instead of dot notation

在postgresql文档中:https://www.postgresql.org/docs/9.1/static/xfunc-sql.html

  

另一种选择是使用功能表示法来提取属性。解释这个的简单方法是我们可以交替使用notations属性(table)和table.attribute。

答案 1 :(得分:1)

这不是答案 - 用它来扩展OP的样本。它似乎与聚合函数无关:

t=# create table s91("count" int);
CREATE TABLE
Time: 38.981 ms
t=# insert into s91 values (1),(2),(3);
INSERT 0 3
Time: 13.929 ms
t=# select count(base.*) from s91 base;
 count 
-------
     1
     2
     3
(3 rows)

t=# alter table s91 rename COLUMN a to "manah_manah";
ALTER TABLE
Time: 1.025 ms
t=# select manah_manah(s91.*) from s91;
 manah_manah 
-------------
           1
           2
           3
(3 rows)

更新:似乎column(alias_name)是一种有效的语法:

s=# with c(a,b) as (values(1,2),(2,3))
select a(c),(c).a from c;
 a | a 
---+---
 1 | 1
 2 | 2
(2 rows)