要获取字段值小于5个字符的行,您需要使用LENGTH()
函数。语法如下-
SELECT *FROM yourTableName WHERE LENGTH(yourColumnName) < 5;
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table fieldLessThan5Chars -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> yourZipCode varchar(10) -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into fieldLessThan5Chars(yourZipCode) values('35801'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('3580'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('90001'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('100'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('10'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('0'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('90209'); mysql> insert into fieldLessThan5Chars(yourZipCode) values('33124');
使用select语句显示表中的所有记录。查询如下-
mysql> select *from fieldLessThan5Chars;
以下是输出-
+----+-------------+ | Id | yourZipCode | +----+-------------+ | 1 | 35801 | | 2 | 3580 | | 3 | 90001 | | 4 | 100 | | 5 | 10 | | 6 | 0 | | 7 | 90209 | | 8 | 33124 | +----+-------------+ 8 rows in set (0.00 sec)
这是查询以获取字段值小于5个字符的所有行-
mysql> select *from fieldLessThan5Chars where length(yourZipCode) < 5;
输出结果
+----+-------------+ | Id | yourZipCode | +----+-------------+ | 2 | 3580 | | 4 | 100 | | 5 | 10 | | 6 | 0 | +----+-------------+ 4 rows in set (0.00 sec)