简体   繁体   English

MySQL联接查询结果按标签过滤

[英]MySQL join query result filtered by tags

I am struggling with one MySQL query and I can figure it out. 我正在努力与一个MySQL查询,我可以弄清楚。 I have three tables containing info about url addresses. 我有三个表,其中包含有关网址的信息。

One, 'domains', stores unique domain names, which is 'parent' table to the second table, 'urls', storing unique urls name with some statistics. 第一个是“域”,存储唯一的域名,第二个表是“父”表,“父”表是“ URL”,其中存储了具有一些统计信息的唯一URL名称。 To each url record can be added multiple tags, which are stored in 'tags' table and there is of course connecting table between urls and tags called 'url_has_tag'. 可以在每个url记录中添加多个标签,这些标签存储在“标签”表中,当然在url和标签之间有一个连接表,称为“ url_has_tag”。

I have query that selects domain name, url stats and concatenated tags for all url records: 我有查询,它为所有url记录选择域名,url统计信息和串联的标签:

SELECT u.url_id, u.url, d.domain, u.hits, u.version, GROUP_CONCAT( t.label) as tags
     FROM urls AS u
     INNER JOIN domains AS d 
        ON (u.parent_id = d.domain_id) 
    LEFT JOIN url_has_tag AS ut ON u.url_id = ut.url_id 
    LEFT JOIN tags as t ON ut.tag_id = t.tag_id
GROUP BY u.url_id
ORDER BY u.hits DESC

Now I need to add filtering results by tags, which means I want to select select the same information but only for urls which are tagged with some tag. 现在,我需要按标签添加过滤结果,这意味着我想选择选择相同的信息,但仅适用于带有某些标签的网址。 They can be tagged by other too, and I want to have the other tags included in the result. 它们也可以被其他人标记,我希望结果中包含其他标记。

When I added WHERE clause before grouping WHERE ut.tag_id = 7 it selects right rows, but the other tags are not included (as expected). 当我在将WHERE ut.tag_id = 7分组之前添加了WHERE子句时,它选择了正确的行,但是未包括其他标签(如预期的那样)。

Could anyone help me with this? 有人可以帮我吗? Thank you very much. 非常感谢你。

By adding where clause on right side table of left outer join makes it inner join. 通过在左侧外部联接的右侧表上添加where子句,可以使其成为内部联接。 So add the ut.tag_id = 7 in the ON clause of join as below. 因此,如下所示在join的ON子句中添加ut.tag_id = 7。

SELECT u.url_id, u.url, d.domain, u.hits, u.version, GROUP_CONCAT( t.label) as tags
 FROM urls AS u
 INNER JOIN domains AS d 
    ON (u.parent_id = d.domain_id) 
LEFT JOIN url_has_tag AS ut ON u.url_id = ut.url_id and ut.tag_id = 7
LEFT JOIN tags as t ON ut.tag_id = t.tag_id
GROUP BY u.url_id
ORDER BY u.hits DESC

Solved thanks to a friend! 解决了,感谢朋友!

SELECT u.url_id, u.url, d.domain, u.hits, u.version, GROUP_CONCAT(DISTINCT(t2.label)) as tags
     FROM urls AS u
     INNER JOIN domains AS d
        ON (u.parent_id = d.domain_id)
     LEFT JOIN url_has_tag AS ut ON u.url_id = ut.url_id
     LEFT JOIN url_has_tag AS ut2 ON u.url_id = ut2.url_id
     LEFT JOIN tags as t ON ut.tag_id = t.tag_id
     LEFT JOIN tags as t2 ON ut2.tag_id = t2.tag_id
WHERE t.tag_id IN (7,8)
GROUP BY u.url_id
ORDER BY u.hits DESC

Tags names have to be DISTINCT, because when url matches several tags from WHERE .. IN clause, its tag names would be concatenated multiple times. 标签名称必须是DISTINCT,因为当url与WHERE .. IN子句中的多个标签匹配时,其标签名称将被多次连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM