MySQL - 计算字符串中包含的id

时间:2013-07-10 07:16:01

标签: mysql sql search longtext

我的表名为“asd”

我有这个架构:

id        |  date     |  content |
AUTO_INC    DATETIME    LONGTEXT

现在假设content = 45,67,89,3,45,5

我在search COUNT()这个表中WHERE content CONTAINS 89怎么办?

我尝试了SELECT COUNT() FROM asd WHERE content IN(89);,但我没有得到任何结果。

5 个答案:

答案 0 :(得分:6)

您可以使用FIND_IN_SET

SELECT COUNT(*) FROM `table` WHERE FIND_IN_SET(89,`content`);

P.S。你需要了解多对多关系。

答案 1 :(得分:1)

试试这个

SELECT COUNT(*)
FROM   ASD
WHERE ',' + content  + ','  like '%,89,%'

答案 2 :(得分:1)

你可以这样试试....我在这个链接中看到了你想要的东西http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/ ..希望它有帮助

  SELECT SUM(IF(content = 89, 1,0)) AS `MATCHED VALUE`,
    COUNT(content) AS `total`
    FROM asd

答案 3 :(得分:0)

SELECT COUNT(*)
FROM   ASD
WHERE  content LIKE '%,89,%' OR
       content LIKE '%,89' OR
       content LIKE '89,%'

%,89,% - 在内容中间匹配89

%,89 - 在内容的最后

89,% - 内容的开头

答案 4 :(得分:0)

SELECT COUNT(*) as cnt FROM `asd` WHERE `content` LIKE '%,89,%' OR  `content` LIKE '89,%' OR `content` LIKE '%,89';

检查SQL小提琴:http://sqlfiddle.com/#!2/151c7/2/0