MySQL查询以字符串字段中的数字字符对行进行分组?
为此,您可以在+运算符的帮助下将0与字符串字段连接起来。这里的场景就像我们需要从字符串字段“9844Bob”中获取数字“9844”。
让我们首先创建一个表-
mysql> create table DemoTable ( StudentId varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('9844Bob');
mysql> insert into DemoTable values('6375DavidMiller');
mysql> insert into DemoTable values('007');
mysql> insert into DemoTable values('97474BobBrown');
mysql> insert into DemoTable values('9844Bob56Taylor');使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------------+ | StudentId | +-----------------+ | 9844Bob | | 6375DavidMiller | | 007 | | 97474BobBrown | | 9844Bob56Taylor | +-----------------+ 5 rows in set (0.00 sec)
以下是根据字符串字段中的数字字符对行进行分组的查询-
mysql> select StudentId,0+StudentId as GroupValue from DemoTable;
这将产生以下输出-
+-----------------+------------+ | StudentId | GroupValue | +-----------------+------------+ | 9844Bob | 9844 | | 6375DavidMiller | 6375 | | 007 | 7 | | 97474BobBrown | 97474 | | 9844Bob56Taylor | 9844 | +-----------------+------------+ 5 rows in set, 4 warnings (0.00 sec)
热门推荐
10 婚礼西装祝福语简短男士
11 友谊晚上祝福语大全简短
12 祝寿时祝福语简短精辟
13 结婚对白誓言简短祝福语
14 别人过大寿祝福语简短
15 护士辞职送行祝福语简短
16 过节文案成语祝福语简短
17 描写周末的祝福语简短
18 宝宝周岁敬酒祝福语简短