创建视图报错:ERROR 1111 (HY000): Invalid use of group function

我们从mysql中使用mysqldump把一个库导出来,带有6个视图,然后通过
mysql -u root -p -h 1.1.1.1 -P 4000 dbname < /xxx/xxx/dbname.sql导入了tidb

然后我们发现6个视图中,有一个没的导入,报了ERROR 1111 (HY000): Invalid use of group function

ccmc_view_tables_phone_status_time这个视图没有导入成功,手工在tidb中创建也报错了。
而ccmc_view_tables_text_status_time这个视图导入成功了。

这两个视图基本上是一样的,如下,我们通过diff的对比
image

两个 视图的定义如下:
CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW ccmc_view_tables_text_status_time AS select saas_cube_xyzphp.tables.code AS tables_code,saas_cube_xyzphp.tables.name AS name,saas_cube_xyzphp.tables.avatar AS avatar,reception.serving_user_num AS chat_busy,saas_cube_xyzphp.tables.chat_limit AS chat_limit,saas_cube_xyzphp.tables.queue_limit AS queue_limit,saas_cube_xyzphp.tables_infos.job_position AS job_position,tables_groups.name AS group_name,tables_status_time.now_status AS now_status,tables_status_time.now_keep_time AS now_keep_time,tables_status_time.online_time AS online_time,tables_status_time.offline_time AS offline_time,tables_status_time.hangup_time AS hangup_time,tables_status_time.acw_time AS acw_time,tables_status_time.acw_num AS acw_num,tables_status_time.callin_acw_time AS callin_acw_time,tables_status_time.callin_acw_num AS callin_acw_num,tables_status_time.callout_acw_time AS callout_acw_time,tables_status_time.callout_acw_num AS callout_acw_num,tables_status_time.hold_time AS hold_time,tables_status_time.rest_time AS rest_time,tables_status_time.rest_num AS rest_num,round((tables_status_time.rest_time / tables_status_time.login_time),4) AS rest_rate,tables_status_time.call_time AS call_time,tables_status_time.callin_time AS callin_time,tables_status_time.callout_time AS callout_time,tables_status_time.login_time AS login_time,tables_status_time.now_at AS now_at,tables_status_time.first_login_at AS first_login_at,tables_status_time.first_logout_at AS first_logout_at,tables_status_time.last_logout_at AS last_logout_at,tables_status_time.first_offline_at AS first_offline_at,tables_status_time.last_offline_at AS last_offline_at from ((((saas_cube_xyzphp.tables left join (select b.tables_code AS tables_code,(select a.current_status from saas_cube_xyzphp.tables_statuses a where (a.id = max(b.id))) AS now_status,(unix_timestamp(now()) - unix_timestamp(max(b.created_at))) AS now_keep_time,sum(if((b.current_status = ‘online’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS online_time,sum(if((b.current_status = ‘offline’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS offline_time,sum(if((b.current_status = ‘hangup’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS hangup_time,sum(if((b.current_status = ‘acw’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS acw_time,count(if((b.current_status = ‘acw’),TRUE,NULL)) AS acw_num,sum(if(((b.current_status = ‘acw’) and (b.last_status = ‘callin’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callin_acw_time,count(if(((b.current_status = ‘acw’) and (b.last_status = ‘callin’)),TRUE,NULL)) AS callin_acw_num,sum(if(((b.current_status = ‘acw’) and (b.last_status = ‘callout’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callout_acw_time,count(if(((b.current_status = ‘acw’) and (b.last_status = ‘callout’)),TRUE,NULL)) AS callout_acw_num,sum(if((b.current_status = ‘hold’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS hold_time,sum(if((b.current_status = ‘rest’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS rest_time,count(if((b.current_status = ‘rest’),TRUE,NULL)) AS rest_num,sum(if(((b.current_status = ‘callout’) or (b.current_status = ‘callin’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS call_time,sum(if((b.current_status = ‘callin’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callin_time,sum(if((b.current_status = ‘callout’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callout_time,sum(if(((b.current_status = ‘online’) or (b.current_status = ‘acw’) or (b.current_status = ‘hold’) or (b.current_status = ‘hangup’) or (b.current_status = ‘rest’) or (b.current_status = ‘callout’) or (b.current_status = ‘callin’) or (b.current_status = ‘offline’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS login_time,now() AS now_at,min(if((b.current_status = ‘login’),b.created_at,NULL)) AS first_login_at,min(if((b.current_status = ‘logout’),b.created_at,NULL)) AS first_logout_at,max(if((b.current_status = ‘logout’),b.created_at,NULL)) AS last_logout_at,min(if((b.current_status = ‘offline’),b.created_at,NULL)) AS first_offline_at,max(if((b.current_status = ‘offline’),b.created_at,NULL)) AS last_offline_at from saas_cube_xyzphp.tables_statuses b where ((b.created_at > if((curtime() < ‘09:00:00’),date_format((curdate() - interval 1 day),‘%Y-%m-%d 18:00:00’),date_format(curdate(),‘%Y-%m-%d 00:00:00’))) and (b.created_at < if((curtime() < ‘09:00:00’),date_format(curdate(),‘%Y-%m-%d 09:00:00’),date_format((curdate() + interval 1 day),‘%Y-%m-%d 00:00:00’))) and (b.type = 1)) group by b.tables_code) tables_status_time on((tables_status_time.tables_code = saas_cube_xyzphp.tables.code))) left join (select saas_cube_xyzphp.tables_reception_states.serving_user_num AS serving_user_num,saas_cube_xyzphp.tables_reception_states.tables_code AS tables_code from saas_cube_xyzphp.tables_reception_states where ((saas_cube_xyzphp.tables_reception_states.created_at > date_format(curdate(),‘%Y-%m-%d 00:00:00’)) and (saas_cube_xyzphp.tables_reception_states.created_at <= date_format(curdate(),‘%Y-%m-%d 23:59:59’)))) reception on((reception.tables_code = saas_cube_xyzphp.tables.code))) left join saas_cube_xyzphp.tables_infos on((saas_cube_xyzphp.tables_infos.code = saas_cube_xyzphp.tables.code))) left join (select saas_cube_xyzphp.tables_group.code AS code,saas_cube_xyzphp.groups.id AS id,saas_cube_xyzphp.groups.name AS name from (saas_cube_xyzphp.tables_group left join saas_cube_xyzphp.groups on((saas_cube_xyzphp.groups.id = saas_cube_xyzphp.tables_group.group_id)))) tables_groups on((tables_groups.code = saas_cube_xyzphp.tables.code)))
这个成功了

CREATE ALGORITHM=UNDEFINED SQL SECURITY DEFINER VIEW ccmc_view_tables_phone_status_time AS select saas_cube_xyzphp.tables.code AS tables_code,saas_cube_xyzphp.tables.name AS name,saas_cube_xyzphp.tables.avatar AS avatar,reception.serving_user_num AS chat_busy,saas_cube_xyzphp.tables.chat_limit AS chat_limit,saas_cube_xyzphp.tables.queue_limit AS queue_limit,saas_cube_xyzphp.tables_infos.job_position AS job_position,tables_groups.name AS group_name,tables_status_time.now_status AS now_status,tables_status_time.now_keep_time AS now_keep_time,tables_status_time.online_time AS online_time,tables_status_time.offline_time AS offline_time,tables_status_time.hangup_time AS hangup_time,tables_status_time.acw_time AS acw_time,tables_status_time.acw_num AS acw_num,tables_status_time.callin_acw_time AS callin_acw_time,tables_status_time.callin_acw_num AS callin_acw_num,tables_status_time.callout_acw_time AS callout_acw_time,tables_status_time.callout_acw_num AS callout_acw_num,tables_status_time.hold_time AS hold_time,tables_status_time.rest_time AS rest_time,tables_status_time.rest_num AS rest_num,round((tables_status_time.rest_time / tables_status_time.login_time),4) AS rest_rate,tables_status_time.call_time AS call_time,tables_status_time.callin_time AS callin_time,tables_status_time.callout_time AS callout_time,tables_status_time.login_time AS login_time,tables_status_time.now_at AS now_at,tables_status_time.first_login_at AS first_login_at,tables_status_time.first_logout_at AS first_logout_at,tables_status_time.last_logout_at AS last_logout_at,tables_status_time.first_offline_at AS first_offline_at,tables_status_time.last_offline_at AS last_offline_at from ((((saas_cube_xyzphp.tables left join (select b.tables_code AS tables_code,(select a.current_status from saas_cube_xyzphp.tables_statuses a where (a.id = max(b.id))) AS now_status,(unix_timestamp(now()) - unix_timestamp(max(b.created_at))) AS now_keep_time,sum(if((b.current_status = ‘online’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS online_time,sum(if((b.current_status = ‘offline’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS offline_time,sum(if((b.current_status = ‘hangup’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS hangup_time,sum(if((b.current_status = ‘acw’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS acw_time,count(if((b.current_status = ‘acw’),TRUE,NULL)) AS acw_num,sum(if(((b.current_status = ‘acw’) and (b.last_status = ‘callin’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callin_acw_time,count(if(((b.current_status = ‘acw’) and (b.last_status = ‘callin’)),TRUE,NULL)) AS callin_acw_num,sum(if(((b.current_status = ‘acw’) and (b.last_status = ‘callout’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callout_acw_time,count(if(((b.current_status = ‘acw’) and (b.last_status = ‘callout’)),TRUE,NULL)) AS callout_acw_num,sum(if((b.current_status = ‘hold’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS hold_time,sum(if((b.current_status = ‘rest’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS rest_time,count(if((b.current_status = ‘rest’),TRUE,NULL)) AS rest_num,sum(if(((b.current_status = ‘callout’) or (b.current_status = ‘callin’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS call_time,sum(if((b.current_status = ‘callin’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callin_time,sum(if((b.current_status = ‘callout’),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS callout_time,sum(if(((b.current_status = ‘online’) or (b.current_status = ‘acw’) or (b.current_status = ‘hold’) or (b.current_status = ‘hangup’) or (b.current_status = ‘rest’) or (b.current_status = ‘callout’) or (b.current_status = ‘callin’) or (b.current_status = ‘offline’)),if(isnull(b.continuous_time),(unix_timestamp(now()) - unix_timestamp(b.created_at)),b.continuous_time),0)) AS login_time,now() AS now_at,min(if((b.current_status = ‘login’),b.created_at,NULL)) AS first_login_at,min(if((b.current_status = ‘logout’),b.created_at,NULL)) AS first_logout_at,max(if((b.current_status = ‘logout’),b.created_at,NULL)) AS last_logout_at,min(if((b.current_status = ‘offline’),b.created_at,NULL)) AS first_offline_at,max(if((b.current_status = ‘offline’),b.created_at,NULL)) AS last_offline_at from saas_cube_xyzphp.tables_statuses b where ((b.created_at > if((curtime() < ‘09:00:00’),date_format((curdate() - interval 1 day),‘%Y-%m-%d 18:00:00’),date_format(curdate(),‘%Y-%m-%d 00:00:00’))) and (b.created_at < if((curtime() < ‘09:00:00’),date_format(curdate(),‘%Y-%m-%d 09:00:00’),date_format((curdate() + interval 1 day),‘%Y-%m-%d 00:00:00’))) and (b.type = 2)) group by b.tables_code) tables_status_time on((tables_status_time.tables_code = saas_cube_xyzphp.tables.code))) left join (select saas_cube_xyzphp.tables_reception_states.serving_user_num AS serving_user_num,saas_cube_xyzphp.tables_reception_states.tables_code AS tables_code from saas_cube_xyzphp.tables_reception_states where ((saas_cube_xyzphp.tables_reception_states.created_at > date_format(curdate(),‘%Y-%m-%d 00:00:00’)) and (saas_cube_xyzphp.tables_reception_states.created_at <= date_format(curdate(),‘%Y-%m-%d 23:59:59’)))) reception on((reception.tables_code = saas_cube_xyzphp.tables.code))) left join saas_cube_xyzphp.tables_infos on((saas_cube_xyzphp.tables_infos.code = saas_cube_xyzphp.tables.code))) left join (select saas_cube_xyzphp.tables_group.code AS code,saas_cube_xyzphp.groups.id AS id,saas_cube_xyzphp.groups.name AS name from (saas_cube_xyzphp.tables_group left join saas_cube_xyzphp.groups on((saas_cube_xyzphp.groups.id = saas_cube_xyzphp.tables_group.group_id)))) tables_groups on((tables_groups.code = saas_cube_xyzphp.tables.code)))

这个不成功

方便的话麻烦上传一下相关的表结构信息,我们本地复现一下

另外 TiDB 集群版本是多少?

CREATE TABLE tables_group ( id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘主键’, code varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘工程师code’, group_id int(11) NOT NULL COMMENT ‘所属小组id’, created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, PRIMARY KEY (id) )

CREATE TABLE tables ( code varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘编号’, name varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘姓名’, avatar varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ‘’ COMMENT ‘头像’, chat_limit smallint(5) unsigned NOT NULL DEFAULT 5 COMMENT ‘’, queue_limit smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT ‘’, text_state varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT ‘’ COMMENT ‘定义’, phone_state varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT ‘’ COMMENT ‘定义’, status tinyint(4) NOT NULL DEFAULT 1 COMMENT ‘状态:1启用;0关闭’, last_login_time datetime DEFAULT NULL COMMENT ‘最后登录时间’, created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, PRIMARY KEY (code) )

CREATE TABLE tables_infos ( code varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘编号’, name varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘姓名’, address varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT ‘’ COMMENT ‘工作所在地’, sex char(4) COLLATE utf8mb4_unicode_ci DEFAULT ‘未知’ COMMENT ‘性别’, phone varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘手机号码’, email varchar(200) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘邮箱’, created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, job_position varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘(职务)’, cost_center varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, line_tag varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, spcsrhc int(11) DEFAULT NULL COMMENT ‘’, first_tier int(11) DEFAULT NULL COMMENT ‘’, lift_telephone_status varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, contract_status varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, company varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘公司’, sign_practice_date datetime DEFAULT NULL COMMENT ‘’, sign_outsource_date datetime DEFAULT NULL COMMENT ‘’, sign_outsource_date_yg datetime DEFAULT NULL COMMENT ‘’, sign_regular_date datetime DEFAULT NULL COMMENT ‘’, training_begin_date datetime DEFAULT NULL COMMENT ‘’, piece_wage_date datetime DEFAULT NULL COMMENT ‘’, children int(11) DEFAULT NULL COMMENT ‘’, school_type varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘/985’, labour_contract_begin_date datetime DEFAULT NULL COMMENT ‘’, labour_contract_end_date datetime DEFAULT NULL COMMENT ‘’, band varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘Band’, comment_level varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, comment_score int(11) DEFAULT NULL COMMENT ‘’, comment_rank int(11) DEFAULT NULL COMMENT ‘’, pay_card varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, emergency_contact varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, emergency_contact_relation varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, emergency_contact_info varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, status int(11) DEFAULT 1 COMMENT ‘状态:1启用;0关闭’, person_id varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, profession_description varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’, photo varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘头像’, PRIMARY KEY (code) )

tables_status_time

tables_groups

这连个表的表结构没有,麻烦也上传一下

另外 TiDB 集群版本是多少的?

tables_groups这是一个别名,如下:

tables_status_time也是一个别名,如下:

请问是哪个版本?

v4.0.0

你好,

  1. 在使用第一个 view 复现你的问题时,创建失败,其中 tables_statuses 表不存在,检查提供的信息中也不存在该表结构信息,请提供下

image
image

  1. 使用第二条 view 同样的问题:tables_statuses 表结构不存在。

CREATE TABLE tables_statuses (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
tables_code varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘编号’,
last_status varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘’,
current_status varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘’,
continuous_time varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘’,
elk_failure enum(‘YES’,‘NO’) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ‘NO’ COMMENT ‘’,
type smallint(5) unsigned NOT NULL DEFAULT ‘1’ COMMENT ‘状态类型: 1:文本状态 2:电话状态’,
created_at timestamp NULL DEFAULT NULL,
updated_at timestamp NULL DEFAULT NULL,
PRIMARY KEY (id),
KEY tables_statuses_created_at_tables_code_index (created_at,tables_code),
KEY created_at (created_at,type,tables_code)
) ENGINE=InnoDB AUTO_INCREMENT=79552 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

你好,

辛苦提供下 sql mode,select @@sql_mode;

mysql [(none)]> select @@sql_mode;
±------------------------------------------------------------------------------------------------------------------------------------------+
| @@sql_mode |
±------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
±------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

hi, saas_cube_xyzphp.tables_reception_states, saas_cube_xyzphp.groups

这 2 张表的建表语句可以提供一下吗

哦哦,不用了~ 我们自己构造了 2 个建表语句

CREATE TABLE tables_reception_states ( id int(10) unsigned NOT NULL AUTO_INCREMENT, tables_code varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘编号’, reception_state enum(‘1’,‘2’) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT ‘1: 空闲 2: 忙碌’, serving_user_num smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT ‘’, chat_limit smallint(5) unsigned NOT NULL DEFAULT 0 COMMENT ‘’, created_at timestamp NULL DEFAULT NULL, updated_at timestamp NULL DEFAULT NULL, PRIMARY KEY (id), KEY tables_reception_states_created_at_index (created_at) )

groups这个是别名

hi,应该是触发了 tidb 的 bug

我建了个 github issue 记录简化后的问题 https://github.com/pingcap/tidb/issues/17852

需要查一下

嗯嗯,感谢,那我们等新版本

ok,目前该问题会在 4.0.2 解决。