0

我有以下与此问题相关的表格:

project
- projectId (PK)
- projectTitle
- projectDescription
- etc..

tag
- tagId
- tagName
- tagDescription
- etc...

project_tag
- projectId (PK / FK -> project.projectId)
- tagId (PK / FK -> tag.tagId)

我正在实现类似于 StackOverflow 的标签功能,因为它能够查看由一个或多个标签标记的项目列表(在我的情况下是项目)。我要做的是选择所有项目,这些项目至少带有我提供给查询的所有标签,但同时检索每个单独项目的所有标签。

我现在的工作,但我觉得WHERE IN子句中的子查询效率很低,因为这可能会为每一行执行,对吗?

SELECT
    `project`.*,
    GROUP_CONCAT( DISTINCT `tagName` ORDER BY `tagName` SEPARATOR ' ' ) as `tags`
FROM
    `project`
JOIN
    `project_tag`
    USING ( `projectId` )
JOIN
    `tag`
    USING ( `tagId` )
WHERE
    `projectId` IN (
        SELECT
            `projectId`
        FROM
            `project_tag`
        JOIN
            `tag`
            USING ( `tagId` )
        WHERE
            `tagName` IN ( 'the', 'tags' )
        GROUP BY
            `projectId`
        HAVING
            COUNT( DISTINCT `tagName` ) = 2 # the amount of tags in the IN clause
    )
GROUP BY
    `projectId`

有什么方法可以加入,tag以便我能够同时检索ed 项目的所有标签,同时只ing 项目(至少)匹配我提供给查询的所有标签,而不必使用该子句?JOINJOINWHERE IN

为了说明示例结果,请考虑以下示例项目:

projectId: 1, tags: php, cms, webdevelopment
projectId: 2, tags: php, cms, ajax
projectId: 3, tags: c#, cms, webdevelopment

搜索标签phpcms产生(这些没有格式化为实际的mysql查询结果,只是为了说明相关数据):

projectId: 1, tags: php, cms, webdevelopment
projectId: 2, tags: php, cms, ajax

不只是:

projectId: 1, tags: php, cms
projectId: 2, tags: php, cms
4

1 回答 1

1

子查询是不相关的(可以取出并自行执行而不会出错),因此应该执行一次。

有一个子查询首先排除不匹配的项目,然后再加入其他表可能会更有效。像这样的东西: -

SELECT project.*, GROUP_CONCAT( DISTINCT tagName ORDER BY tagName SEPARATOR ' ' ) as tags
FROM (SELECT projectId, COUNT( DISTINCT tagName ) AS TagCount
        FROM tag
        INNER JOIN project_tag USING (tagId)
        WHERE tagName IN ( 'the', 'tags' )
        GROUP BY projectId
        HAVING TagCount = 2) Sub1
INNER JOIN project ON Sub1.projectId = project.projectId
INNER JOIN project_tag USING (projectId)
INNER JOIN tag USING (tagId)
GROUP BY projectId

我假设您在 tagName 上有一个索引。

于 2013-04-03T12:42:55.900 回答