【问题标题】:SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'xxx' for key 'group_key'SQLSTATE [23000]:违反完整性约束:1062 键 'group_key' 的重复条目 'xxx'
【发布时间】:2014-01-17 11:48:21
【问题描述】:

在我的网页上没有插入或更新,但我已经收到此错误:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0-Bekoo-2449.10' for key 'group_key'

这很令人惊讶,因为网页没有修改任何行,并且Product 表没有group_key 列或键。

我的查询:

SELECT Product._like,
       comment_count,
       title,
       price_lower,
       price,image,
       AffiliateOffers.name,
       p‌​‌​ayout_yuzde,
       payout_nakit,
       payout_type,
       xml_id,
       brand,model,
       currency,url,
       Product.af‌​f_‌​id,
       Product.offer_id,
       pb_share_1,
       pb_share_2,
       pb_share_1_payda,
       pb_share_2_payda,
       A‌​ffil‌​iateOffers.seo_title,
       afo_offer_id,
       r_category,
       seo_description 
FROM Product 
inner Join AffiliateOffers on Product.aff_id = AffiliateOffers.af_id 
      AND Product.offer_id = AffiliateOffers.af_offer_id 
where Product.status = 1 
    and Product.aff_id = 1 
    and Product.offer_id = 1290

还有我的表结构:

` CREATE TABLE `_product` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `xml_id` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `aff_id` int(11) NOT NULL,
  `offer_id` int(11) NOT NULL,
  `r_category` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  `category_id1` int(11) NOT NULL,
  `category_id2` int(11) NOT NULL,
  `category_id3` int(11) NOT NULL,
  `brand` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `model` varchar(75) COLLATE utf8_unicode_ci DEFAULT NULL,
  `title` mediumtext COLLATE utf8_unicode_ci NOT NULL,
  `description_1` mediumtext COLLATE utf8_unicode_ci,
  `price` decimal(18,2) NOT NULL,
  `price_lower` decimal(18,2) NOT NULL,
  `percentage_lower` int(11) NOT NULL,
  `image` mediumtext COLLATE utf8_unicode_ci NOT NULL,
  `url` text COLLATE utf8_unicode_ci,
  `feature` tinyint(4) NOT NULL,
  `city` varchar(250) COLLATE utf8_unicode_ci DEFAULT NULL,
  `sex` tinyint(4) NOT NULL,
  `stock` tinyint(4) NOT NULL,
  `start_date` int(25) NOT NULL,
  `finish_date` int(25) NOT NULL,
  `update_date` int(25) NOT NULL,
  `status` tinyint(4) NOT NULL DEFAULT '1',
  `hit` int(11) NOT NULL DEFAULT '1',
  `_like` int(11) NOT NULL DEFAULT '0',
  `comment_count` int(11) DEFAULT NULL,
  `like_count` int(11) DEFAULT NULL,
  `lastlike_time` int(25) DEFAULT NULL,
  `visit_count` int(11) DEFAULT NULL,
  `lastvisit_count` int(25) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `xml_id` (`xml_id`),
  KEY `lastlike_time` (`lastlike_time`),
  KEY `offer_id` (`offer_id`),
  KEY `r_category` (`r_category`),
  KEY `status` (`status`)
) ENGINE=MyISAM AUTO_INCREMENT=3816867 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci`

为什么会出现这个错误?

【问题讨论】:

  • 我添加了一个编辑。没有“group_key”键。
  • (主键或唯一索引)具有 3 个唯一字段。某处必须有写操作。也许是触发器?
  • 在我的桌子上只有一个键和它的主要 (ID) 。不,没有写操作,也没有触发
  • 刚刚发布:你的表结构(完整的SHOW CREATE TABLE)和你的查询

标签: mysql


【解决方案1】:

除非您另有说明,否则group_key 是 MySQL 在聚合事物时使用的内部字段,因此您不会在自己的表或视图中找到它。那里的重复输入错误意味着您的聚合存在某种问题。

查看您正在使用的任何 GROUP BY 语句,并确保您的语法正确,尤其是在任何聚合函数和组表达式中。

【讨论】:

  • 您是否在使用 DISTINCTMAXAVG 之类的聚合函数?
  • 不,看我的查询。 SELECT Product._like,comment_count,title,price_lower,price,image,AffiliateOffers.name,payout_yuzde,payout_nakit,payout_type,xml_id,brand,model,currency,url,Product.aff_id,Product.offer_id,pb_share_1,pb_share_2,pb_share_1_payda,pb_share_2_payda,AffiliateOffers.seo_title,afo_offer_id,r_category,seo_description FROM Product inner Join AffiliateOffers on Product.aff_id = AffiliateOffers.af_id AND Product.offer_id = AffiliateOffers.af_offer_id where Product.status = 1 and Product.aff_id = '1' and Product.offer_id = '1290'
  • offer_idaff_idINT 类型,因此您不应像使用 = '1290'= '1' 那样将它们与字符串进行比较
  • 我做到了,但我认为这与错误无关?但是再次感谢
  • 我发现了问题,它是关于不同的。我想获得 group by 或区分品牌、价格和 r_category,但我不能用 group by 来做,因为它太慢了。查询:SELECT distinct r_category as r_category,brand as brand,price as price FROM Product where Product.status = 1 and Product.aff_id = 1 and Product.offer_id = 1290
【解决方案2】:

在 Phpmyadmin 中,您必须截断所有日志表。 那应该可以解决它。

我遇到了同样的问题,我忘记清除访问者和在线日志。 这两个表都为用户生成一个日志/ID(这就是 XXX 代表的位置) 如果您截断两个表,它将解决此问题。

【讨论】:

    猜你喜欢
    • 2013-04-22
    • 2016-01-16
    • 2014-09-16
    • 2015-10-28
    • 2012-04-20
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    相关资源
    最近更新 更多