在MySQL中获取第一个字符不是字母数字的行?
要获取第一个字符不是字母数字的行,可以使用以下正则表达式。
情况1-如果您想要以数字开头的行,则可以使用以下语法-
SELECT *FROM yourTableName WHERE yourColumnName REGEXP '^[0-9]';
情况2-如果您希望这些以字母数字开头的行,请使用以下语法-
SELECT *FROM yourTableName WHERE yourColumnName REGEXP '^[^0-9A-Za-z]' ;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table getRowsFirstNotAlphanumeric -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserPassword varchar(20), -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('@123456'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('#7666666'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('98876Carol'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('$12345Carol'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('%David567'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('123456Larry'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('909Robert'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('3333Maxwell'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('_123456Bob'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('5767676Chris'); mysql> insert into getRowsFirstNotAlphanumeric(UserPassword) values('(88883Mike');
现在,您可以使用select语句显示表中的所有记录。查询如下-
mysql> select *from getRowsFirstNotAlphanumeric;
以下是输出-
+----+--------------+ | Id | UserPassword | +----+--------------+ | 1 | @123456 | | 2 | #7666666 | | 3 | 98876Carol | | 4 | $12345Carol | | 5 | %David567 | | 6 | 123456Larry | | 7 | 909Robert | | 8 | 3333Maxwell | | 9 | _123456Bob | | 10 | 5767676Chris | | 11 | (88883Mike | +----+--------------+ 11 rows in set (0.00 sec)
情况1-这里是查询以获取不是以字母数字开头的所有行-
mysql> SELECT *FROM getRowsFirstNotAlphanumeric -> WHERE UserPassword REGEXP '^[0-9]';
以下是输出-
+----+--------------+ | Id | UserPassword | +----+--------------+ | 3 | 98876Carol | | 6 | 123456Larry | | 7 | 909Robert | | 8 | 3333Maxwell | | 10 | 5767676Chris | +----+--------------+ 5 rows in set (0.00 sec)
情况2:这是获取所有以字母数字开头的行的查询:
mysql> SELECT *FROM getRowsFirstNotAlphanumeric -> WHERE UserPassword REGEXP '^[^0-9A-Za-z]';
以下是输出:
+----+--------------+ | Id | UserPassword | +----+--------------+ | 1 | @123456 | | 2 | #7666666 | | 4 | $12345Carol | | 5 | %David567 | | 9 | _123456Bob | | 11 | (88883Mike | +----+--------------+ 6 rows in set (0.00 sec)