我们如何从 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)
热门推荐
10 弟弟退伍祝福语简短
11 端午销售祝福语简短英文
12 简短的古风生日祝福语
13 送老师新春祝福语简短
14 长辈侄女结婚祝福语简短
15 送弟妹住院祝福语简短
16 企业老板文案祝福语简短
17 伴娘前夫结婚祝福语简短
18 爸妈对宝宝祝福语简短