douwen7603
2014-02-21 08:17
浏览 143
已采纳

从mysql中的逗号分隔字符串中获取特定值

I have an issue that I am stuck in,

I have a table called wpg_original_word with field tags, which contains comma separated values and what I want to achieve is search for tags which are starting with the letter 'd'

I have written the query

SELECT DISTINCT tags FROM wpg_original_word  WHERE tags LIKE 'd%' ;

It returns the whole string like 'Diabetes, Nutrition, Dietician' , but I want only the Diabetes, Dietician.

Any help?

图片转代码服务由CSDN问答提供 功能建议

我有一个问题,我被困在了,

我有一个 带有字段标记的 wpg_original_word 表,其中包含逗号分隔值,我想要实现的是搜索以'd' \开头的标记 n

我编写了查询

  SELECT DISTINCT tags FROM wpg_original_word WHERE tags LIKE'd%'; 
   
  
 

它返回整个字符串,如'Diabetes,Nutrition,Dietician',但我只想要 Diabetes,Dietician

任何帮助?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsfgdsjfd78773 2014-02-21 08:32
    已采纳

    You could split the string with PHP, use the following code

    <?php
    
    $string = 'Diabetes, Nutrition, Dietician';
    $array = explode(',', $string);
    $result = array();
    foreach ($array as $part) {
        if (substr(strtolower(trim($part)),0,1) == 'd') {
            array_push($result, trim($part));
        }
    }
    
    var_dump($result);
    

    See it in action here. http://codepad.org/SVk7FPx9

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongluan6784 2014-02-21 08:26

    You can use in_array() to check for a string in your query and then print it.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题