For conditions, use IF(). Let us first create a table −
mysql> create table DemoTable637 (ClientId int NOT NULL AUTO_INCREMENT PRIMARY KEY,ClientName varchar(100),ClientAge int ); Query OK, 0 rows affected (0.82 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable637(ClientName,ClientAge) values('Chris',23); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable637(ClientName,ClientAge) values('Robert',24); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable637(ClientName,ClientAge) values('David',57); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable637(ClientName,ClientAge) values('Carol',60); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable637;
This will produce the following output −
+----------+------------+-----------+ | ClientId | ClientName | ClientAge | +----------+------------+-----------+ | 1 | Chris | 23 | | 2 | Robert | 24 | | 3 | David | 57 | | 4 | Carol | 60 | +----------+------------+-----------+ 4 rows in set (0.00 sec)
Following is the query to set special characters if condition is true −
mysql> select if(ClientName IS NOT NULL AND ClientAge > 50,ClientName,'-------------') from DemoTable637;
This will produce the following output −
+--------------------------------------------------------------------------+ | if(ClientName IS NOT NULL AND ClientAge > 50,ClientName,'-------------') | +--------------------------------------------------------------------------+ | ------------- | | ------------- | | David | | Carol | +--------------------------------------------------------------------------+ 4 rows in set (0.00 sec)