0

我目前有使用 PDO 的 SELECT 但我收到此错误消息

SELECT a.*, UNIX_TIMESTAMP(a.createdon) AS 时间戳, ct.cityname, ct.SeoCityName, scat.subcatname, scat.SeoSubcatName,
               COUNT(*) AS piccount, p.picfile, UNIX_TIMESTAMP(feat.featuredtill) AS featurestill
            FROM prefix_ads 一个
                 INNER JOIN prefix_cities ct ON a.cityid = ct.cityid
           INNER JOIN prefix_subcats scat ON a.subcatid = scat.subcatid
          左外连接 prefix_adxfields axf ON a.adid = axf.adid
                左外连接 prefix_adpics p ON a.adid = p.adid AND p.isevent = '0'        
                LEFT OUTER JOIN prefix_featured feat ON a.adid = feat.adid AND feat.adtype = 'A'        
            其中 a.catid = '10'
              AND a.enabled = '1' AND a.verified = '1' AND a.expireson >= NOW()   
            由 a.adid 分组
            ORDER BY a.createdon DESC
            限制 0, 18

SQLSTATE [23000]:违反完整性约束:1062 键 'group_key' 的重复条目 'xxxx'

xxxx = 曾经是列表中的 if

有人可以帮忙吗?

解释

大批
(
    [id] => 1
    [0] => 1
    [select_type] => 简单
    [1] => 简单
    [表] => scat
    [2] => 粪便
    [类型] => 全部
    [3] => 全部
    [possible_keys] => 初级
    [4] => 初级
    [关键] =>
    [5] =>
    [key_len] =>
    [6] =>
    [参考] =>
    [7] =>
    [行] => 152
    [8] => 152
    [过滤] => 100.00
    [9] => 100.00
    [额外] => 使用临时;使用文件排序
    [10] => 使用临时的;使用文件排序
)
大批
(
    [id] => 1
    [0] => 1
    [select_type] => 简单
    [1] => 简单
    [表] => 一个
    [2] => 一个
    [类型] => 参考
    [3] => 参考
    [possible_keys] => subcatid,cityid,已验证,启用,expireson,catid,ct_ads
    [4] => subcatid,cityid,已验证,启用,expireson,catid,ct_ads
    [key] => subcatid
    [5] => subcatid
    [key_len] => 2
    [6] => 2
    [参考] => DB.scat.subcatid
    [7] => DB.scat.subcatid
    [行] => 150
    [8] => 150
    [过滤] => 100.00
    [9] => 100.00
    [额外] => 使用 where
    [10] => 使用 where
)
大批
(
    [id] => 1
    [0] => 1
    [select_type] => 简单
    [1] => 简单
    [表] => ct
    [2] => ct
    [类型] => eq_ref
    [3] => eq_ref
    [possible_keys] => 初级
    [4] => 初级
    [键] => 初级
    [5] => 初级
    [key_len] => 2
    [6] => 2
    [参考] => DB.a.cityid
    [7] => DB.a.cityid
    [行] => 1
    [8] => 1
    [过滤] => 100.00
    [9] => 100.00
    [额外] =>
    [10] =>
)
大批
(
    [id] => 1
    [0] => 1
    [select_type] => 简单
    [1] => 简单
    [表格] => axf
    [2] => axf
    [类型] => 参考
    [3] => 参考
    [possible_keys] => 添加
    [4] => 添加
    [键] => 添加
    [5] => 添加
    [key_len] => 4
    [6] => 4
    [参考] => DB.a.adid
    [7] => DB.a.adid
    [行] => 1
    [8] => 1
    [过滤] => 100.00
    [9] => 100.00
    [额外] => 使用索引
    [10] => 使用索引
)
大批
(
    [id] => 1
    [0] => 1
    [select_type] => 简单
    [1] => 简单
    [表格] => p
    [2] => p
    [类型] => 参考
    [3] => 参考
    [possible_keys] => 添加
    [4] => 添加
    [键] => 添加
    [5] => 添加
    [key_len] => 5
    [6] => 5
    [参考] => DB.a.adid,const
    [7] => DB.a.adid,常量
    [行] => 1
    [8] => 1
    [过滤] => 100.00
    [9] => 100.00
    [额外] =>
    [10] =>
)
大批
(
    [id] => 1
    [0] => 1
    [select_type] => 简单
    [1] => 简单
    [表格] => 壮举
    [2] => 壮举
    [类型] => eq_ref
    [3] => eq_ref
    [possible_keys] => 添加
    [4] => 添加
    [键] => 添加
    [5] => 添加
    [key_len] => 7
    [6] => 7
    [参考] => DB.a.adid,const
    [7] => DB.a.adid,常量
    [行] => 1
    [8] => 1
    [过滤] => 100.00
    [9] => 100.00
    [额外] =>
    [10] =>
)
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE scat ALL PRIMARY NULL NULL NULL 152 使用临时;使用文件排序
1 SIMPLE a ref subcatid,cityid,verified,enabled,expireson,catid,c... subcatid 2 DB.scat.subcatid 150 使用 where
1 简单 ct eq_ref PRIMARY PRIMARY 2 DB.a.cityid 1   
1 SIMPLE axf ref adid adid 4 DB.a.adid 1 使用索引
1 SIMPLE p ref adid adid 5 DB.a.adid,const 1   
1 简单的壮举 eq_ref adid adid 7 DB.a.adid,const 1   

问候 G

4

1 回答 1

0

您的查询不正确。
您不应该正确使用那么多选择字段和分组依据。

例如:

SELECT                                                                                                  
    UNIX_TIMESTAMP(a.createdon) AS timestamp,                                                               
    ct.cityname,                                                                                            
    ct.SeoCityName,                                                                                         
    scat.subcatname,                                                                                        
    scat.SeoSubcatName,                                                                                     
    p.picfile,                                                                                              
    UNIX_TIMESTAMP(feat.featuredtill) AS featuredtill,                                                      
    COUNT(*) AS piccount                                                                                    
FROM                                                                                                    
    prefix_ads a                                                                                          
    INNER JOIN prefix_cities ct ON a.cityid = ct.cityid                                    
    INNER JOIN prefix_subcats scat ON a.subcatid = scat.subcatid                                 
    LEFT OUTER JOIN prefix_adxfields axf ON a.adid = axf.adid                                     
    LEFT OUTER JOIN prefix_adpics p ON a.adid = p.adid AND p.isevent = '0'                  
    LEFT OUTER JOIN prefix_featured feat ON a.adid = feat.adid AND feat.adtype = 'A'        
WHERE
    a.catid = '10'                                                                        
    AND a.enabled = '1' 
    AND a.verified = '1' 
    AND a.expireson >= NOW()                         
GROUP BY 
    1,2,3,4,5,6,7                                                                             
ORDER BY 
    a.createdon DESC                                                                   
LIMIT 0, 18

由于 COUNT 是一个分组函数,它不会继续 'group by' 子句

于 2014-04-16T18:35:52.920 回答