对于条件,请使用IF()
。让我们首先创建一个表-
mysql> create table DemoTable637 (ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,ClientName varchar(100),ClientAge int );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable637(ClientName,ClientAge) values('Chris',23); mysql> insert into DemoTable637(ClientName,ClientAge) values('Robert',24); mysql> insert into DemoTable637(ClientName,ClientAge) values('David',57); mysql> insert into DemoTable637(ClientName,ClientAge) values('Carol',60);
使用select语句显示表中的所有记录-
mysql> select *from DemoTable637;
这将产生以下输出-
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 1 | Chris | 23 | | 2 | Robert | 24 | | 3 | David | 57 | | 4 | Carol | 60 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
以下是在条件为true时设置特殊字符的查询-
mysql> select if(ClientName IS NOT NULL AND ClientAge > 50,ClientName,'-------------') from DemoTable637;
这将产生以下输出-
+--------------------------------------------------------------------------+ | if(ClientName IS NOT NULL AND ClientAge > 50,ClientName,'-------------') | +--------------------------------------------------------------------------+ | ------------- | | ------------- | | David | | Carol | +--------------------------------------------------------------------------+ 4 rows in set (0.00 sec)