将文章标题与另一个 table 中列出的标签相匹配
match article title with tags listed in another table
我需要 mysql 中的查询,它可以列出一个 table 中匹配的 tag_names 和另一个 table[=14= 中的 article_title 列]
tags table
------------
tag_id tag_name
--------------
1 travel
2 tickets
3 business
4 america
article table
-------------
article_id article_title
--------- --------------
1 travel tips to america
2 cheap tickets for favorite destinations
3 prices for business class tickets to america
expected output
--------------
article_id tag_id tag_name
---------- ------- ----------
1 1 travel
1 4 america
2 2 tickets
3 3 business
3 2 tickets
3 4 america
按如下方式连接两个表:
SELECT article_id, tag_id, tag_name
FROM tags as t
LEFT JOIN articles as a
ON t.tag_name LIKE concat('%',a.article_title,'%')
查询应该如下:
SELECT a.article_id, t.tag_id, t.tag_name
FROM article a
JOIN tags t
ON a.article_title LIKE CONCAT('%', t.tag_name, '%')
ORDER BY a.article_id;
但是,如果您想用 space 标记标签,您应该将查询的第 4 行替换为
ON a.article_title LIKE CONCAT('% ', t.tag_name, ' %')
这不会考虑像
这样的标题中的标签 america
美国梦是美国的民族精神
执行此操作的最佳方法是使用第三个 table 来放置标签和文章 ID。
tags table
------------
tag_id tag_name
--------------
1 travel
2 tickets
3 business
4 america
article table
-------------
article_id article_title
--------- --------------
1 travel tips to america
2 cheap tickets for favorite destinations
3 prices for business class tickets to america
article_tags table
------------
at_id tag_id article_id
--------------
1 1 1
2 4 1
3 2 2
4 3 2
这样,您可以为所有文章和所有标签维护一个 table,而无需为每篇文章的每个标签保留冗余行。
因此,如果您想获取 article_id 1
的所有标签
SELECT at.id, t.tag_id, t.tag_name FROM article_tags at
INNER JOIN articles a ON (a.article_id = at.article_id)
INNER JOIN tags t ON (at.tag_id = t.tag_id)
WHERE at.article_id = 1
该查询应该输出,
at_id tag_id tag_name
1 1 travel
2 4 america
或者,您可能想要获取特定标签的所有文章,例如 "tickets"
SELECT a.article_name FROM article_tags at
INNER JOIN articles a ON (at.article_id = a.article_id)
INNER JOIN tags t ON (at.tag_id = t.tag_id)
WHERE t.tag_name = 'tickets'
应该输出:
article_name
---------------
cheap tickets for favorite destinations
prices for business class tickets to america
我需要 mysql 中的查询,它可以列出一个 table 中匹配的 tag_names 和另一个 table[=14= 中的 article_title 列]
tags table ------------ tag_id tag_name -------------- 1 travel 2 tickets 3 business 4 america
article table ------------- article_id article_title --------- -------------- 1 travel tips to america 2 cheap tickets for favorite destinations 3 prices for business class tickets to america
expected output -------------- article_id tag_id tag_name ---------- ------- ---------- 1 1 travel 1 4 america 2 2 tickets 3 3 business 3 2 tickets 3 4 america
按如下方式连接两个表:
SELECT article_id, tag_id, tag_name
FROM tags as t
LEFT JOIN articles as a
ON t.tag_name LIKE concat('%',a.article_title,'%')
查询应该如下:
SELECT a.article_id, t.tag_id, t.tag_name
FROM article a
JOIN tags t
ON a.article_title LIKE CONCAT('%', t.tag_name, '%')
ORDER BY a.article_id;
但是,如果您想用 space 标记标签,您应该将查询的第 4 行替换为
ON a.article_title LIKE CONCAT('% ', t.tag_name, ' %')
这不会考虑像
这样的标题中的标签 america美国梦是美国的民族精神
执行此操作的最佳方法是使用第三个 table 来放置标签和文章 ID。
tags table
------------
tag_id tag_name
--------------
1 travel
2 tickets
3 business
4 america
article table
-------------
article_id article_title
--------- --------------
1 travel tips to america
2 cheap tickets for favorite destinations
3 prices for business class tickets to america
article_tags table
------------
at_id tag_id article_id
--------------
1 1 1
2 4 1
3 2 2
4 3 2
这样,您可以为所有文章和所有标签维护一个 table,而无需为每篇文章的每个标签保留冗余行。
因此,如果您想获取 article_id 1
的所有标签SELECT at.id, t.tag_id, t.tag_name FROM article_tags at
INNER JOIN articles a ON (a.article_id = at.article_id)
INNER JOIN tags t ON (at.tag_id = t.tag_id)
WHERE at.article_id = 1
该查询应该输出,
at_id tag_id tag_name
1 1 travel
2 4 america
或者,您可能想要获取特定标签的所有文章,例如 "tickets"
SELECT a.article_name FROM article_tags at
INNER JOIN articles a ON (at.article_id = a.article_id)
INNER JOIN tags t ON (at.tag_id = t.tag_id)
WHERE t.tag_name = 'tickets'
应该输出:
article_name
---------------
cheap tickets for favorite destinations
prices for business class tickets to america