根据另一个字段中的布尔值获取单个字段的 MySQL 查询
我们先创建一个表 -
mysql> create table DemoTable ( EmployeeId int NOT NULL AUTO_INCREMENT PRIMARY KEY, EmployeeName varchar(40), isMarried boolean ); Query OK, 0 rows affected (0.57 sec)
使用 insert 命令在表中插入一些记录 -
mysql> insert into DemoTable(EmployeeName,isMarried) values('Chris',true); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(EmployeeName,isMarried) values('Robert',false); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(EmployeeName,isMarried) values('Mike',false); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(EmployeeName,isMarried) values('Bob',true); Query OK, 1 row affected (0.07 sec) mysql> insert into DemoTable(EmployeeName,isMarried) values('Tom',true); Query OK, 1 row affected (0.12 sec)
使用 select 语句从表中显示所有记录 -
mysql> select *from DemoTable;
这将产生以下输出 -
+------------+--------------+-----------+ | EmployeeId | EmployeeName | isMarried | +------------+--------------+-----------+ | 1 | Chris | 1 | | 2 | Robert | 0 | | 3 | Mike | 0 | | 4 | Bob | 1 | | 5 | Tom | 1 | +------------+--------------+-----------+ 5 rows in set (0.00 sec)
根据另一个字段中的布尔值获取单个字段的查询如下 -
mysql> select EmployeeName from DemoTable where isMarried=true;
这将产生以下输出 -
+--------------+ | EmployeeName | +--------------+ | Chris | | Bob | | Tom | +--------------+ 3 rows in set (0.00 sec)
广告