2017-08-18 93 views
2

我不明白爲什麼這個查詢:名稱爲'count'的Count列返回多行。爲什麼?

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 
+0

簡單地說'select count(*)from mytable'有什麼問題? – jarlh

+0

沒什麼,我只是不明白爲什麼會出現這種情況。 – RobAu

+0

好問題。該查詢甚至不是有效的ANSI SQL語法。 – jarlh

回答

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。

+0

感謝您的鏈接! –

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) 
+0

感謝您的額外努力!看起來'表達'是使用對列名稱的偏好進行分析的。它以某種方式忽略了括號 - 部分,但我還沒有在Postgresql的'gram.y'中找到它 – RobAu

+0

更新後的示例 - 現在看起來像一個功能,而不是bug :) –

+0

有趣的:)我當然可以切換到' count(1)'爲我的應用程序..我希望這種行爲可能最終在文檔中以某種方式 – RobAu

相關問題