FIND_IN_SET 函数

find_in_set(str, strlist)

功能描述

strlist 是由 N 个被 , 分隔的子串组成的字符串。如果字符串 strstrlist 中,则返回 str 的位置(1 到 N 之间)。

如果 str 不在 strlist 中或 strlist 为空字符串,则返回 0。

参数说明

  • str:STRING 类型,待查找的字符串。
  • strlist:STRING 类型,以逗号分隔的字符串列表。

返回结果

INT 类型。返回 strstrlist 中的位置(从 1 开始),未找到返回 0。

使用示例

  1. 基本用法:

    SELECT find_in_set('b', 'a,b,c,d');
    -- 结果:2
  2. 未找到时返回 0:

    SELECT find_in_set('d', 'a,b,c');
    -- 结果:0
  3. 支持中文字符:

    SELECT find_in_set('二', '一,二,三');
    -- 结果:2

注意事项

  • 当任一参数为 NULL 时,返回 NULL。
  • 该函数在第一个参数 str 包含逗号 , 时将无法正常工作。
  • 空字符串作为 str 时返回 0。

联系我们
预约咨询
微信咨询
电话咨询