查询数据库表字段以查找字符串

时间:2016-06-03 14:40:37

标签: mysql

我有一个表格字段' new_options'可以包含后续类型的数据:

  1. 包括string-a
  2. 在内的东西
  3. 包括string-b
  4. 在内的东西
  5. 什么都没有
  6. 其他不包括string-a或string-b
  7. 的内容

    我想设置$ options_exist =' no'如果满足条件1,2或3。 或$ options_exist ='是'如果满足条件4。

    到目前为止,我有这个适用于1,3和4:

    $str='string-a';
    $opts_exist = strpos($product[new_options], $str);    
    if ($opts_exist == true) {
            $options_exist='no';
     }
    elseif ($product[new_options]==''){
    $options_exist='no';
    } else {
    $options_exist='yes';
    }
    

    我如何为string-b添加条件?

1 个答案:

答案 0 :(得分:0)

在SQL中执行:

SELECT CASE WHEN new_options LIKE '%string-a%' THEN 'no'
            WHEN new_options LIKE '%string-b%' THEN 'no'
            WHEN new_options = '' THEN 'no'
            ELSE 'yes'
       END AS options_exist
FROM yourTable
相关问题