0

我从不同的表中获取记录并避免重复行我使用GROUP BY子句,DISTINCTGROUP BY我尝试使用(作为替换)的大量JOIN查询要快,但它也获取重复记录。知道如何实现优化、最快和高效的查询。DISTINCTGROUP BY

查询样本:

这里索引是projectId在项目表中的字段上设置的。

SELECT DISTINCT p.*, p.projectId as proId, plat.*, con.*, dom.*, sta.*, tech.*, role.*, pimg.*, c.*, dur.* 
                FROM projects p 
                LEFT JOIN projectplatforms pplat ON p.projectId = pplat.projectId JOIN platforms plat ON plat.platformId = pplat.platformId 
                LEFT JOIN projectcountries pcon ON p.projectId = pcon.projectId JOIN countries con ON con.countryId = pcon.countryId 
                LEFT JOIN projectdomains pdom ON p.projectId = pdom.projectId JOIN domains dom ON dom.domainId = pdom.domainId 
                LEFT JOIN statuses sta ON p.statusId = sta.statusId 
                LEFT JOIN projecttechs ptech ON p.projectId = ptech.projectId JOIN technologies tech ON tech.techId = ptech.techId 
                LEFT JOIN projectroles prole ON p.projectId = prole.projectId JOIN roles role ON role.roleId = prole.roleId 
                LEFT JOIN projectimages pimg ON p.projectId = pimg.projectId
                LEFT JOIN clients c ON p.clientId = c.clientId 
                LEFT JOIN durations dur ON p.durationId = dur.durationId 
                WHERE p.featured = 1 
                ORDER BY p.year DESC
4

1 回答 1

0

使用 DISTINCT mysql 将返回所有唯一的行。如果您在一个或部分列上使用 GROUP BY,情况就不一样了。您可能想使用 GROUP BY 而不是 DISTINCT。

于 2012-10-16T09:46:18.373 回答