Usage of sql coalesce() function

  • use:

Replace empty values ​​with other values

Returns the first non-empty value

  • expression:

COALESCE is a function, (expression_1, expression_2, ..., expression_n) refers to each parameter expression in turn, stops when encountering a non-null value and returns the value. If all expressions are null values, a null value will eventually be returned. The use of COALESCE is that most expressions that contain a null value will eventually return a null value

  • Instance

There is an industry code table, as follows, there are four levels of industry code yjbm, two-level code ejbm, three-level code sajbm, four-level code sijbm

How to judge which level an industry belongs to?

First of all, the level of this industry code belongs to a tree structure, for example:

A
A01
A011
A012
A013
A014


B
B0
B01
B011
B012
B013

The specific industry code query that returns the industry is as follows

select COALESCE(sijbm,sajbm,ejbm,yjbm) as hybm,* from ioc_market.m_dr_tz_hyzd where hymc ='采矿业'

result:

Guess you like

Origin blog.csdn.net/qq_34050399/article/details/107632306