doujiepin5547
doujiepin5547
2016-10-01 17:03

mySQL - FIND_IN_SET()找不到值

已采纳

I m trying to get posts which includes a spesific tag.

The tag row content

,iphone|1468338028,,android|1468338028,,blackberry|1468338028,

query

SELECT * FROM shares WHERE FIND_IN_SET(tag, 'iphone') > 0 ORDER BY DATE DESC limit 10

What is the correct way to do it ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dpp3047 dpp3047 5年前

    Your tag is iphone|1468338028 and you look for iphone. That does not match.
    Replace the | with , to separate the values.

    SELECT * FROM shares 
    WHERE FIND_IN_SET(replace(tag, '|', ','), 'iphone') > 0 
    
    点赞 评论 复制链接分享
  • douwan2664 douwan2664 5年前

    Another alternative is to use LIKE "%text%", if you're not required to use FIND_IN_SET().

    SELECT * FROM shares 
    WHERE tag LIKE "%iphone%"
    ORDER BY DATE DESC limit 10
    

    Above snippet should achieve the same, thus avoiding replacing and trimming issues.

    点赞 评论 复制链接分享

为你推荐