Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用计数聚合创建MySQL视图_Mysql_Sql_Function_Count - Fatal编程技术网

使用计数聚合创建MySQL视图

使用计数聚合创建MySQL视图,mysql,sql,function,count,Mysql,Sql,Function,Count,我正在使用GROUPBY子句创建一个标准的COUNT查询 CREATE VIEW view1 AS SELECT t2.column3 , t1.id AS t1_id , t2.column4 , COUNT ( t3.id ) AS t3_count , -- Error here COUNT ( t4.id ) AS t4_count FROM t

我正在使用
GROUPBY
子句创建一个标准的
COUNT
查询

CREATE VIEW view1 AS
SELECT
    t2.column3                  ,
    t1.id AS t1_id              ,
    t2.column4                  ,
    COUNT ( t3.id ) AS t3_count , -- Error here
    COUNT ( t4.id ) AS t4_count
FROM      table1 t1
LEFT JOIN table2 t2 ON t2.column1 = 25 AND
                       t2.column2 = t1.id
LEFT JOIN table3 t3 ON t3.column1 = t1.id
LEFT JOIN table4 t4 ON t4.column1 = t1.id
GROUP BY t2.column3 ,
         t1.id      ,
         t2.column4 ;
但这会产生一个错误:

Error Code: 1630. FUNCTION mydb.COUNT does not exist.
为什么MySQL认为我在试图调用数据库中的用户定义函数?
它是否将
COUNT
识别为内置聚合操作

我还尝试了
COUNT(t3.*.
,但出现了一个通用语法错误。
我无法使用
COUNT(*)
,因为有多个
左连接同时计数。

我将删除空格:

CREATE VIEW view1 AS
SELECT
    t2.column3                  ,
    t1.id AS t1_id              ,
    t2.column4                  ,
    COUNT(t3.id) AS t3_count , -- Error here
    COUNT(t4.id) AS t4_count
FROM      table1 t1
LEFT JOIN table2 t2 ON t2.column1 = 25 AND
                       t2.column2 = t1.id
LEFT JOIN table3 t3 ON t3.column1 = t1.id
LEFT JOIN table4 t4 ON t4.column1 = t1.id
GROUP BY t2.column3 ,
         t1.id      ,
         t2.column4 ;

SELECT COUNT(*)
-- 1

SELECT COUNT (*)
-- You have an error in your SQL syntax; 

SELECT COUNT ( * )
-- You have an error in your SQL syntax; 

增编: 如前所述,这种行为可以改变:

set session sql_mode = concat(@@sql_mode, ',IGNORE_SPACE');

SELECT COUNT ( * )
-- 1

@Giffyguy此行为是可配置的。见@PaulSpiegel非常感谢!很高兴知道。我必须查看可用的可配置选项阵列,看看还有什么。