MySQL POSITION函数


本MySQL教程通过语法和示例说明了如何使用MySQL POSITION函数。

描述

MySQL POSITION函数用于返回字符串中子字符串的位置。

语法

MySQL中POSITION函数的语法为:

1
POSITION( substring IN string )
参数 说明
substring 要在string中搜索的子字符串。
string 要搜索的字符串。
start_position 可选的。用于指定从string中哪个位置开始查找。默认为1,即字符串中的第一个位置。

说明

  • 字符串中的第一个位置是1。
  • 如果在string中找不到substring,则POSITION函数将返回0。
  • 查找时,不区分大小写的。

POSITION函数和LOCATE函数的作用是一样的。

示例

让我们看一些MySQL POSITION函数示例,并探索如何在MySQL中使用POSITION函数。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
mysql> SELECT POSITION('s', 'igiftidea.com');
Result: 1
mysql> SELECT POSITION('abc', 'igiftidea.com');
Result: 0
mysql> SELECT POSITION('S', 'igiftidea.com');
Result: 1
mysql> SELECT POSITION('ever', 'igiftidea.com');
Result: 4
mysql> SELECT POSITION('e', 'igiftidea3', 3);
Result: 4
mysql> SELECT POSITION(4, 'sea4evergreen');
Result: 4
mysql> SELECT POSITION(null, 'igiftidea.com');
Result: null
mysql> SELECT POSITION(false, 'igiftidea.com');
Result: 0


原文链接:https://codingdict.com/