【问题标题】:select relevance title based on tag similar to like with mysql根据类似于 mysql 的标签选择相关标题
【发布时间】:2012-10-30 11:21:16
【问题描述】:

标签

tag_id   post_id   value
------------------------
1        1         some
2        1         good
3        1         title
4        2         some
5        2         good
6        3         some
7        4         good
8        4         title

帖子

post_id  title
-------------------
1        some good title
2        some good
3        some
4        good title

我们怎样才能得到 post_id = 1 和 2 的值 somegood 在同一个 post_id 中?

所以结果是

RESULT
title
----------
some good title
some good

good title dosent 显示,因为标签中的 post_id = 4 中没有 some 值。 some 不显示要求 good

【问题讨论】:

  • this你的帐户?
  • 不,顺便说一句,它非常相似。这就是链接

标签: mysql sql search relevance


【解决方案1】:

多次尝试LIKE

SELECT * FROM post
WHERE title LIKE '%some%'
AND title LIKE '%good%'

See this SQLFiddle

您也可以像这样连接两个表:

SELECT post.post_id, title FROM Post
RIGHT JOIN Tags
ON post.post_id = tags.post_id
WHERE Tags.value IN ('some','good')
GROUP BY post.Post_ID
HAVING COUNT(*)>1;

See this SQLFiddle

注意:如果我们不使用HAVING子句,它也会返回任何单个值存在的记录

See this SQLFiddle

See the similar requirement with similar table structure.

【讨论】:

  • 性能怎么样?我们可以使用标签吗?还是我们最好不需要这样做?
  • 谢谢他,你能解释一下 count(*)>1 有什么作用吗?
  • 如果我们不使用HAVING,它也会给我们包含任何一个值的记录(例如一些,好)See this SQLFiddle没有HAVING
猜你喜欢
  • 1970-01-01
  • 2016-07-26
  • 2013-08-03
  • 1970-01-01
  • 2017-02-21
  • 1970-01-01
  • 2014-02-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多