Sql query. If there is no data in a field, the default data is set.

use sql to query the database, and if there is no data in the field, set the data in the field to a certain value? How to write such sql ?

Dec.23,2021

take it apart and do it, it is impossible to do it with a single SELECT;
add a sub-statement, and the IF function docks UPDATE.

Old Daili has only these two methods for the time being.


1:

SELECT RegName,
       RegEmail,
       RegPhone,
       RegOrg,
       RegCountry,
       DateReg,
       ISNULL(Website,'no website')  AS WebSite 
FROM   RegTakePart 
WHERE  Reject IS NULL

2:

SELECT RegName,
       RegEmail,
       RegPhone,
       RegOrg,
       RegCountry,
       DateReg,
       COALESCE(Website,'no website')  AS WebSite 
FROM   RegTakePart 
WHERE  Reject IS NULL

via: Giannis Paraskevopoulos


mysql> select * from test2;
+----+------+
| id | id2  |
+----+------+
| -1 |   -1 |
|  0 | NULL |
|  1 |    1 |
|  2 |    2 |
|  3 |    3 |
|  5 |    5 |
+----+------+
6 rows in set (0.00 sec)

mysql> select id,ifnull(id2,"") from test2;
+----+-------------------+
| id | ifnull(id2,"")  |
+----+-------------------+
| -1 | -1                |
|  0 |                 |
|  1 | 1                 |
|  2 | 2                 |
|  3 | 3                 |
|  5 | 5                 |
+----+-------------------+
6 rows in set (0.00 sec)

I wonder if I can help you


ifnull (columnValue,defaultValue)

Menu