我们如何从 MySQL 表中的列的值中提取子字符串?
我们可以应用任何函数,如SUBSTRING(),MID()或SUBSTR()从列的值中提取子字符串。在这种情况下,我们必须提供列的名称作为函数的第一个参数,即在字符串的位置我们必须提供列的名称。下面的例子将演示它。
例子
假设我们想从 'Student' 表的 'Name' 列中提取一个子字符串,那么可以通过使用不同的函数来完成,如下所示 -
mysql> Select name, SUBSTR(name,2,4) from student;+---------+------------------+
| name | SUBSTR(name,2,4) |
+---------+------------------+
| Gaurav | aura |
| Aarav | arav |
| Harshit | arsh |
| Gaurav | aura |
| Yashraj | ashr |
+---------+------------------+
5 rows in set (0.00 sec)
mysql> Select name, MID(name,2,4) from student;
+---------+---------------+
| name | MID(name,2,4) |
+---------+---------------+
| Gaurav | aura |
| Aarav | arav |
| Harshit | arsh |
| Gaurav | aura |
| Yashraj | ashr |
+---------+---------------+
5 rows in set (0.00 sec)
mysql> Select name, substring(name,2,4) from student;
+---------+---------------------+
| name | substring(name,2,4) |
+---------+---------------------+
| Gaurav | aura |
| Aarav | arav |
| Harshit | arsh |
| Gaurav | aura |
| Yashraj | ashr |
+---------+---------------------+
5 rows in set (0.00 sec)
我们还可以在上述查询中应用条件,如下所示 -
mysql> Select name, substring(name,2,4) from student WHERE address = 'delhi';+---------+---------------------+
| name | substring(name,2,4) |
+---------+---------------------+
| Gaurav | aura |
| Harshit | arsh |
+---------+---------------------+
2 rows in set (0.16 sec)
mysql> Select name, MID(name,2,4) from student WHERE address = 'delhi';
+---------+---------------+
| name | MID(name,2,4) |
+---------+---------------+
| Gaurav | aura |
| Harshit | arsh |
+---------+---------------+
2 rows in set (0.00 sec)
mysql> Select name, SUBSTR(name,2,4) from student WHERE address = 'delhi';
+---------+------------------+
| name | SUBSTR(name,2,4) |
+---------+------------------+
| Gaurav | aura |
| Harshit | arsh |
+---------+------------------+
2 rows in set (0.00 sec)
以上是 我们如何从 MySQL 表中的列的值中提取子字符串? 的全部内容, 来源链接: utcz.com/z/358979.html