MySQL根据列

时间:2016-10-04 04:08:43

标签: mysql mysql-error-1055

我使用的是MySQL 5.7.13

我的数据库中有以下记录,我想根据单列(email_identifier)过滤掉记录。即,如果特定列具有重复值,则仅返回单个记录。

我正在尝试跟踪查询,但它会抛出以下错误

mysql> select * from list_subscriber where list_master_id = 202 group by email_identifier;

错误1055(42000):SELECT列表的表达式#1不在GROUP BY子句中,并且包含非聚合列'db1.list_subscriber.id',它在功能上不依赖于GROUP BY子句中的列;这与sql_mode = only_full_group_by

不兼容

理想情况下,上述查询应仅返回1条记录,包含所有列

请建议我如何实现这一目标?

DATABASE ROW SAMPLE

mysql> select * from list_subscriber where list_master_id = 202 limit 2\G;
*************************** 1. row ***************************
                         id: 30518899
             list_master_id: 202
          unique_identifier: A
           email_identifier: xyz@yahoo.co.in
          mobile_identifier: 9195423432432
alternate_mobile_identifier: 
          region_identifier: 
            city_identifier: 
         country_identifier: 
             age_identifier: 
          gender_identifier: 
                   raw_data: {"user":"A","number":"919542342423","msg":"msg3","emailid":"xyz@vfirst.com"}
                 created_at: 2016-07-08 15:33:24
                 updated_at: 2016-08-11 14:55:52
                 deleted_at: NULL
*************************** 2. row ***************************
                         id: 30518901
             list_master_id: 202
          unique_identifier: J
           email_identifier: xyz@vfirst.com
          mobile_identifier: 919540077166
alternate_mobile_identifier: 
          region_identifier: 
            city_identifier: 
         country_identifier: 
             age_identifier: 
          gender_identifier: 
               raw_data: {"user":"J","number":"919540077166","msg":"msg4","emailid":"xyz@vfirst.com"}
                 created_at: 2016-07-08 15:34:49
                 updated_at: 2016-08-11 14:55:52
                 deleted_at: NULL

更新

CREATE TABLE `list_subscriber` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`list_master_id` int(10) unsigned NOT NULL,
`unique_identifier` varchar(512) COLLATE utf8_unicode_ci NOT NULL,
`email_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`mobile_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`alternate_mobile_identifier` varchar(255) COLLATE utf8_unicode_ci NOT   NULL,
`region_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`city_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`country_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`age_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`gender_identifier` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`raw_data` longtext COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`deleted_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `list_subscriber_list_master_id_foreign` (`list_master_id`),
CONSTRAINT `list_subscriber_list_master_id_foreign` FOREIGN KEY    (`list_master_id`) REFERENCES `list_master` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)   ENGINE=InnoDB AUTO_INCREMENT=66956432 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

2 个答案:

答案 0 :(得分:0)

执行group by时,您需要为每列提供aggregate function,MySQL如何选择两个不同的值之一?

如果您不想强制执行该电子邮件在您的表中是唯一的,那么 应该做什么就是告诉MySQL。最好的方法是add a unique index

alter table list_subscriber add unique key unique_email (email_identifier);

如果您的表中已经有重复项,则可能会失败。要查找具有重复项的所有电子邮件,请执行

select email_identifier, count(1) as cnt from list_subscriber group by email_identifier having cnt > 1;

这将为您提供所有这些电子邮件,然后您将手动删除/合并记录。

答案 1 :(得分:0)

关于分组:

例如:

select a, b,c from abc group by a

在这种情况下,您需要添加聚合函数到列b和c 您可以将列b和c添加到分组,如:

select a,b,c from group by a, b, c