首页 > 文章列表 > 如果 MySQL INSERT() 函数中的插入位置超出范围,会发生什么情况?

如果 MySQL INSERT() 函数中的插入位置超出范围,会发生什么情况?

399 2023-08-26

MySQL INSERT() function performs no insertion if the position of insertion is out of range. The position of insertion can be out of range in the case when we pass a negative or 0(zero) value or the value goes beyond the value of a total number of characters in an original string by 2. It can be understood with the help of the following example −

Example

The query below will perform no insertion because the position of insertion is out of range i.e. a negative value.

mysql> Select INSERT('Virat', -1,5,'Kohli');
+-------------------------------+
| INSERT('Virat', -1,5,'Kohli') |
+-------------------------------+
| Virat                         |
+-------------------------------+
1 row in set (0.00 sec)

下面的查询不会执行插入操作,因为插入位置超出范围,即0(零)。

mysql> Select INSERT('Virat', 0,5,'Kohli');
+------------------------------+
| INSERT('Virat', 0,5,'Kohli') |
+------------------------------+
| Virat                        |
+------------------------------+
1 row in set (0.00 sec)

下面的查询不会执行插入操作,因为插入位置超出范围,即超过原始字符串中字符数量的值2。在下面的示例中,原始字符串'Virat'有5个字符,我们给出的位置值是7,因此不会发生插入。

mysql> Select INSERT('Virat', 7,5,'Kohli');
+------------------------------+
| INSERT('Virat', 7,5,'Kohli') |
+------------------------------+
| Virat                        |
+------------------------------+
1 row in set (0.00 sec)