编写单个MySQL查询以从相应行返回ID,该值是NOT NULL值
让我们首先创建一个表-
mysql> create table DemoTable ( StudentId int, StudentName varchar(100) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values(NULL,'Chris'); mysql> insert into DemoTable values(NULL,'Robert'); mysql> insert into DemoTable values(101,'David'); mysql> insert into DemoTable values(102,'Mike'); mysql> insert into DemoTable values(103,'Sam');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
这将产生以下输出-
+-----------+-------------+ | StudentId | StudentName | +-----------+-------------+ | NULL | Chris | | NULL | Robert | | 101 | David | | 102 | Mike | | 103 | Sam | +-----------+-------------+ 5 rows in set (0.00 sec)
以下是从相应行返回ID的查询,该值是NOTNULL值-
mysql> select StudentId from DemoTable where StudentName='David' and StudentId IS NOT NULL;
这将产生以下输出-
+-----------+ | StudentId | +-----------+ | 101 | +-----------+ 1 row in set (0.00 sec)