Some functions use the mysql concat

  Man today home group_concat, was feeling some here know little, learn a lot.

 A: concat basis functions

1. function usage

  Syntax: concat (str1, str2, ...)

  Connecting a plurality of strings into a single string.

 

2. Example

select concat(contract_id, ' - ', bd_code, ' - ', bd_name) as tmp from t_plm_contract_monitor;

  effect:

  

  It can be seen here is the data in each row, all the fields, be connected.

 

3. Notes

  If a field is null, then return to this line of argument is a null [online].

  experimenting:

  

 

  turn out:

  This line is not empty null, the test of true knowledge, no place is not displayed.

  I do not know version of the problem. This mysql version is 5.6.

 

Two: concat_ws

1. Description

  The above wording feeling a little tired, because each link breaks should write, is there a simple way?

  How this function is as follows:

  concat_ws(separator, str1, str2, ...)

  As long as the first position, specify the delimiter will not be a problem.

 

2. Test

select concat_ws(' - ',contract_id,bd_code,bd_name) as tmp from t_plm_contract_monitor;

  effect:

  

 

 

3. Description

  Feeling of use is very good use.

 

4. less data would happen then?

  Test results are as follows, it is still not displayed.

  

 

 

Three: group_concat

1. Description

  This is today's main event.

 

2. Examples

  By grouping contract id, find the smallest id.

select contract_id, min(id) from t_plm_contract_monitor group by contract_id;

  result:

  

 

 

3. more and more demand

  The above problem is relatively simple, but I want to know a contract_id corresponding id, what it does, and as multi-line contract_id data does not appear.

  This time you can use this function a.

 

Example 4

select contract_id, GROUP_CONCAT(id) from t_plm_contract_monitor group by contract_id;

  result:

  

 

 

The more optimized usage

  Why do you say so, because the above usage, there is no explanation of how to use the delimiter.

  GROUP_CONCAT (Field [order by sort field asc / desc] [distinct] connected to [Separator 'delimiter'])

  

6. to test

  Let id be flashbacks, using the '-' segmentation.

select contract_id, GROUP_CONCAT(distinct id order by id desc separator '-') from t_plm_contract_monitor group by contract_id;

  result:

  

 

 

7. Also one use

select group_concat(mm.contract_id) from (
select contract_id from t_plm_contract_bill_term_monitor where is_deleted = 0
GROUP by bill_term_end_date,contract_id HAVING count(*) > 1
) mm

  result:

  

 

Guess you like

Origin www.cnblogs.com/juncaoit/p/12629858.html