查找MySQL中第二位有空格的特定记录
mysqlmysqli database
为此,请使用 SUBSTR(),如以下语法所示 −
select * from yourTableName where substr(yourColumnName, 2, 1 ) = ' ';
首先我们创建一个表 −
mysql> create table DemoTable1365 -> ( -> Value varchar(20) -> ); Query OK, 0 rows affected (0.66 sec)
使用 insert 命令在表中插入一些记录 −
mysql> insert into DemoTable1365 values('9756757474'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable1365 values('3 45322333'); Query OK, 1 row affected (0.19 sec) mysql> insert into DemoTable1365 values('8974646363'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1365 values('9 566363622'); Query OK, 1 row affected (0.17 sec)
使用 select 语句显示表中的所有记录 −
mysql> select * from DemoTable1365;
这将产生以下输出 −
+-------------+ | Value | +-------------+ | 9756757474 | | 3 45322333 | | 8974646363 | | 9 566363622 | +-------------+ 4 rows in set (0.00 sec)
下面的查询是查找表中仅第二位有空格的特定记录,如"3 4532" −
mysql> select * from DemoTable1365 -> where substr(Value, 2, 1 ) = ' ';
这将产生以下输出 −
+-------------+ | Value | +-------------+ | 3 45322333 | | 9 566363622 | +-------------+ 2 rows in set (0.00 sec)