MySQL相关 'projects'查询

标签 mysql sql

请考虑以下项目表:

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/

相关文章:

mysql - 查找与特定优惠券关联的所有订单

php - 用mysql获取两行记录

php - MySQL 'field list' 中的未知列

MySQL 查询不返回任何行

mysql - 我的 Cygnus 代理没有创建 MySQL 数据库

php - 显示以 utf8 数据库字段结束的 htmlspecials 字符

mysql - 如何在表格上插入日期 ISO 8601?

sql - VS-2017 SSIS 中评估的变量和成功评估的包在 ssms 中执行时不起作用

mysql - 关于哪种方法更好的 SQL-(mysql) 问题

sql - Azure SQL 数据迁移助手卡在 “Migrate Data” 上