小编典典

根据类似于mysql的标签选择相关标题

sql

标签

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 some ,并good在同一POST_ID?

所以结果是

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

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


阅读 195

收藏
2021-04-28

共1个答案

小编典典

尝试LIKE多次:

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

看到这个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;
2021-04-28