case substring

A, case function

1. Simple functions: enumerate all the possible values ​​of this field. Enumeration value for a fixed value.

(1) grammar

case column

  when condition_1 then result_1

  when condition_2 then result_2

  when condition_3 then result_3

  ......

  else result_4

end

(2) Example

MySQL> the SELECT * the FROM case_demo;
+ ---- + ------ + ----- +
| the above mentioned id | name | Sex |
+ ---- + ------ + ---- - +
|. 1 | Zhang |. 1 |
| 2 | John Doe |. 1 |
|. 3 | florets | 2 |
|. 4 | Bob |. 1 |
|. 5 | Pend |. 1 |
|. 6 | Linda | 2 |
|. 7 | Healer | 2 |
| 8 | Lotus | 2 |
+ ---- + ------ + ----- +

MySQL> the SELECT name,
-> the CASE Sex
-> the WHEN. 1 THEN 'M'
-> the WHEN 2 THEN 'F'
-> Sex the END the AS
-> the FROM case_demo;
+ ------ + ----- +
| name | Sex |
+ ------ + ----- +
| Joe Smith | M |
| John Doe | M |
| flowers | F |
| Bob | M |
| Pend | M |
| Linda | female |
| Healer | F |
| Lotus | F |
+ ------ + ----- +

 

2. Search function: You can write the judge, and the search function will only return the first matching value, others caseare ignored. Adaptation expression with a different set of values match, the enumerated value is not a fixed value but a set range .

(1) grammar

case 

  when condition_1 then result_1

  when condition_2 then result_2

  when condition_3 then result_3

  ......

  else result_4

end

(2) Example

mysql> SELECT * FROM grade_demo;

+----+------+-------+

| id | name | grade |

+----+------+-------+

| 1 | Hsiao Ming | 60.5 |

| 2 | Hsiao Ming | 60 |

| 3 | wheat | 78 |

| 4 | Xiaoxia | 80.5 |

| 5 | small constant | 90 |

| 6 | Lotus | 98.5 |

| 7 | Linda | 40.8 |

| 8 | XiaoHe | 55.9 |

| 9 | small cell | 70 |

+----+------+-------+

  

mysql> SELECT name,

    -> CASE 

    -> WHEN grade >=90 THEN '优秀'

    -> WHEN grade >=70 THEN '良好'

    -> WHEN grade >=60 THEN '及格'

    -> ELSE 'fail'

    -> END AS 'rating score'

    -> FROM grade_demo;

+------+----------+

| Name | Score Ratings |

+------+----------+

| Xiao Ming | pass |

| Xiao Ming | pass |

| Wheat | good |

| Xiaoxia | good |

| Small constant | outstanding |

| Lotus | outstanding |

| Linda | fail |

| XiaoHe | fail |

| Small cell | good |

+------+----------+

 

mysql> SELECT name,

-> the CASE
->. 1 THEN the WHEN Sex = 'M'
-> = 2 THEN the WHEN Sex 'F'
-> Sex the END the AS
-> the FROM case_demo;
+ ------ + ----- +
| name | Sex |
+ ------ + ----- +
| Joe Smith | M |
| John Doe | M |
| flowers | F |
| Bob | M |
| Pend | M |
| Linda | F |
| Healer | F |
| Lotus | F |
+ ------ + ----- +

Two, substring function

(1) grammar

  • substring(string,postion,length)
  • subtring(string from position for length)

string: string to extract a substring.

position: is an integer , the starting character strings are designated, position may be a positive or negative integer.

     If the position is positive, then the substring SUBSTRING function extracts from the beginning of the string. Please refer to the following string.

     If the position parameter is zero, then the function returns an empty string SUBSTRING.

     If the position is negative, the SUBSTRING function starting from the end of the string, the substring extraction.

length: length is a positive integer that specifies the number of characters in the substring.

          If the number of characters is greater than the sum of the position and length of the string, SUBSTRING function returns a substring starting position to the end of the string.

          If length is omitted, SUBSTRING function returns a substring from a starting position to the end of the string.

(2) Example

mysql> SELECT SUBSTRING('mysql substring',2,6);

+----------------------------------+

| SUBSTRING('mysql substring',2,6) |

+----------------------------------+ 

| ysql s                           | 

+----------------------------------+

 

mysql> SELECT SUBSTRING('mysql substring',6,4);

+----------------------------------+ 

| SUBSTRING('mysql substring',6,4) |

+----------------------------------+

|  sub                             |

+----------------------------------+

 

mysql> SELECT SUBSTRING('mysql substring',6,15);

+-----------------------------------+

| SUBSTRING('mysql substring',6,15) |

+-----------------------------------+

|  substring                        |

+-----------------------------------+

 

mysql> SELECT SUBSTRING('mysql substring',6);

+--------------------------------+

| SUBSTRING('mysql substring',6) |

+--------------------------------+

|  substring                     |

+--------------------------------+

 

mysql> SELECT SUBSTRING('mysql substring',0,1);

+----------------------------------+

| SUBSTRING('mysql substring',0,1) |

+----------------------------------+

|                                  |

+----------------------------------+

 

mysql> SELECT SUBSTRING('mysql substring',0);

+--------------------------------+

| SUBSTRING('mysql substring',0) |

+--------------------------------+

|                                |

+--------------------------------+

Guess you like

Origin www.cnblogs.com/aczy/p/10930743.html