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

TAGS

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

POSTS

post_id title

-------------------

1 some good title

2 some good

3 some

4 good title

how can we get the post_id = 1 and 2 that contains value some and good in the same post_id?

so the result is

RESULT

title

----------

some good title

some good

good title dosent show becouse there is no some value in post_id = 4 in tags.

some doesnt show beouse the requirement good

解决方案

Try LIKE multiple time:

SELECT * FROM post

WHERE title LIKE '%some%'

AND title LIKE '%good%'

You can also join both tables like this:

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;

Note: If we don't use HAVING clause, It will also return records where any single value exists

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值