Use the keyword COALESCE

COALESCE is a key standard sql inside, we can use together and aggregate functions sum, count, max, etc. to complete some special features. 

The following mysql sql statement based on

a sum of the column 1, a query, or NULL if no data is 0 or
table field has a field: Kilometers (f_gls), count the total number of kilometers
COALESCE (sum(f_gls), 0) distance

 

2, find out the maximum value

Field has a field table: Module id (f_moudleId), queries the maximum

COALESCE(MAX(f_moudleId),0) max_moudleId

 

3, the integrated use: Format

The record repository tables inside, the knowledge base to generate an id

    SELECT
            CONCAT('KB',DATE_FORMAT(NOW(),'%Y%m%d'),LPAD(COALESCE(COUNT(id),0)+1, 3, '0')) <!--格式 KB20190610001-->
        FROM
            user_knowledgebase
        WHERE
            DATEDIFF(DATE(now()), DATE(createtime))= 0

 

Guess you like

Origin www.cnblogs.com/passedbylove/p/11320561.html