我从不同的表中获取记录并避免重复行我使用GROUP BY
子句,DISTINCT
比GROUP BY
快,所以我尝试了大量JOIN
查询DISTINCT
(作为GROUP 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
答案 0 :(得分:0)
使用DISTINCT mysql将返回所有唯一行。如果您在列的一个或部分列表上使用GROUP BY,则不一样。您可能想要使用GROUP BY而不是DISTINCT。