请考虑以下项目表:
Project:
table: project
manyToMany:
themes:
targetEntity: Theme
inversedBy: projects
joinTable:
name: project_theme
joinColumns:
project_id:
referencedColumnName: id
inverseJoinColumns:
theme_id:
referencedColumnName: id
platforms:
targetEntity: Platform
joinTable:
name: project_platform
joinColumns:
project_id:
referencedColumnName: id
inverseJoinColumns:
platform_id:
referencedColumnName: id
manyToOne:
client:
targetEntity: Client
如您所见,一个项目具有三个关系;主题通过 project_theme 连接表,平台通过 project_platform 表,客户端通过 client_id 列。
我正在尝试生成一个查询,该查询将查找所有相关项目 - 具有相同主题、平台或客户的项目 - 并按“分数”对它们进行排序。
例如:
Project A: Themes: 18, 19 Platforms: 1, 4 Client: 22
Find related projects to Project A..
Project D: Themes: 18, 19 Platforms: 1, 4 Client: 22 Score: 5
Project G: Themes: 18, 21 Platforms: 3, 4 Client: 22 Score: 3
Project B: Themes: 8, 21 Platforms: 2, 4 Client: 1 Score: 1
I'd really appreciate some assistant with writing a MySQL query for this. I've been struggling for a while with the following - but I'm probably miles off:
SELECT
`project`.*,
GROUP_CONCAT(`project_theme`.`theme_id`) as themes,
GROUP_CONCAT(`project_platform`.`platform_id`) as platforms,
`project`.`client_id` as client
FROM `project`
LEFT JOIN `project_theme` ON `project`.`id` = `project_theme`.`project_id`
LEFT JOIN `project_platform` ON `project`.`id` = `project_platform`.`project_id`
GROUP BY `project`.`id`
非常感谢您的帮助
皮特
最佳答案
我在平板电脑上,所以我会很简短。将它们视为三个独立的查询并将它们合并。当我有键盘时,我会带着 sql 回来。
关于MySQL相关 'projects'查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13513187/