在这里,我们将使用OCTET_LENGTH来检查记录的长度,因为我们希望记录的长度大于1。让我们首先创建一个表-
create table DemoTable ( UserId int NOT NULL AUTO_INCREMENT PRIMARY KEY, UserName varchar(20), UserGender varchar(20) );
使用插入命令在表中插入一些记录-
insert into DemoTable(UserName,UserGender) values('John','M'); insert into DemoTable(UserName,UserGender) values('Carol','Male'); insert into DemoTable(UserName,UserGender) values('Mia','Female'); insert into DemoTable(UserName,UserGender) values('Emma','F');
以下是使用select语句显示表中所有记录的查询-
select *from DemoTable;
这将产生以下输出-
+--------+----------+------------+ | UserId | UserName | UserGender | +--------+----------+------------+ | 1 | John | M | | 2 | Carol | Male | | 3 | Mia | Female | | 4 | Emma | F | +--------+----------+------------+ 4 rows in set (0.00 sec)
这是我们要获取长度大于1的记录的查询。
select *from DemoTable where OCTET_LENGTH(UserGender) > 1;
这将产生以下输出-
+--------+----------+------------+ | UserId | UserName | UserGender | +--------+----------+------------+ | 2 | Carol | Male | | 3 | Mia | Female | +--------+----------+------------+ 2 rows in set (0.00 sec)