Top ten most commonly used functions in Mysql

Function one: CONCAT

  • Usage scenario: concatenate multiple fields into a string when querying
  • select concat('id:',`id`,',name:',`name`) from `student`
  • Analysis: The fields that need to be connected are separated by commas, and the custom splicing symbols are wrapped in single quotes and separated by commas.
  • Note: This function needs to be noted that if a column value is empty, then the result of the splicing of this column will be null. In order to avoid this situation, you need to add function 2 to the column that may be empty.

Function two: IFNULL          

  • Usage scenario: When querying, judge whether the field is empty, if it is empty, take the second default parameter, if it is not empty, take the data in the table
  • SELECT IFNULL(`student_number`,'is null') FROM `sc_student`

     

  • Analysis: Two parameters, the first parameter is the field name, the second parameter is the default value
  • Note: It is the judgment of the query result. If the data does not exist, the default value will not be taken, such as the following (a total of ten data in the student table): SELECT IFNULL(student_number,'is null') FROM `sc_student` WHERE id = 100, this result is null instead of the default value'is null'

Function three: COALESCE        

  • Usage scenario: When querying, judge whether the field is empty, if the field is empty, take the value that is not empty from the second parameter, and take the data in the table if the field is not empty
  • SELECT COALESCE(`student_number`,NULL,NULL,'is default') FROM `student`

     

  • Analysis: Multiple parameters, the first parameter is the field name, and the second parameter is optional. If the field is empty, the value is the value that is not empty from the second parameter, if all are empty , It returns null
  • Note: No

Function 4: REPLACE

  • Usage scenario: replace the specified characters in the field when querying
  • SELECT REPLACE(`name`,'陈小婷','小红') FROM `student`

  • Analysis: Three parameters, the first parameter is the field name, the second parameter is the string to be replaced in the field, and the third parameter is the replaced string
  • Note: the second parameter value is replaced with the third parameter value

Function five: FIND_IN_SET

  • Usage scenario: Whether the specified character exists in the field, this type of field has a feature, that is, there will be multiple values, separated by',' (comma)
  • SELECT `id` , `name` , `score` from `student` where find_in_set ('100' , `score` )

     

  • Analysis: Two parameters, the first parameter is the query condition, the second parameter is the field that needs to be matched; suppose there is a field score in the student table to store the scores of three courses, which is separated by',' (English comma ), this statement is to query the student table for at least one student's id and name whose score is 100 points
  • Note: The order of the two parameters is often easy to write upside down. The most tricky thing is to write upside down and the program does not report errors...

Function six: CASE

  • Usage scenario: There is a state field in a table, the value is 0 (disabled) or 1 (enabled), but I want to display the corresponding Chinese characters in the query results 
  • SELECT `id` , `nickname` , `state` ,(CASE `state` WHEN 0 THEN '禁用' WHEN 1 THEN '启用' ELSE '其他' END ) `state_new` FROM `student`

  • Solution: Query the id, name and status of the student table. When the status is 0, it will be disabled, if it is 1, it will be enabled, and if it is not 0 and 1, it will display others.
  • Note: There is an end at the end, don’t forget

Function seven: GROUP_CONCAT    

  • Use scenario: If there is a product table, a product label table and a product label association table, I want to find out multiple product label ids corresponding to each product 
  • SELECT pi.id,GROUP_CONCAT( ptr.product_tag_id ) as tags FROM `product_info` pi LEFT JOIN `product_tag_rel` ptr ON pi.id = ptr.product_info_id GROUP BY pi.id

     

  • Analysis: Query each product and the corresponding product tag id of each product (product tag id will be separated by commas)
  • Note: grouping

Function eight: FROM_UNIXTIME

  • Usage scenario: timestamp to date format
  • SELECT `id` , FROM_UNIXTIME(`add_time`, '%Y-%m-%d %H:%i') FROM `student`

     

  • Solution: Query the id of the student table and the formatted creation time (year-month-day hour: minute)
  • Note: the case of format letters

Function nine: TIMESTAMPDIFF

  • Use scenario: find the time difference
  • SELECT TIMESTAMPDIFF(SECOND , `create_time` , `update_time`)  FROM `student`
    
    SELECT TIMESTAMPDIFF(SECOND,'2018-07-01 09:00:00','2018-07-04 12:00:00') FROM `student`

  • Solution: Query the second difference between the creation time and the modification time of the student table (SECOND: second / MINUTE: minute / HOUR: hour / DAY: day / MONTH: month / YEAR: year)
  • Note: create_time <update_time, if the data type is datetime

 

Function ten: MAX

  • Usage scenario: Query the maximum value
  • SELECT `id`, `subject_id` , MAX(score) FROM `student_score` GROUP BY `subject_id` HAVING MAX(score) > 90

  • Analysis: Query the highest score of each subject over 90 points in the student transcript, GROUP BY `subject_id` is used for each subject grouping , HAVING MAX(score)> 90 is used to filter the conditions that exceed 90 points
  • Note: grouping, filtering

Guess you like

Origin blog.csdn.net/weixin_43452467/article/details/113857305