MySQL Notes 2--DQL

Table of contents

The difference between IFNULL function and ISNULL function

Advanced 3. Sorting query

Advanced 4. Common functions

1. Character functions

2. Mathematical functions

3. Date function

4. Other functions

5. Process control function

2. Grouping function


The difference between IFNULL function and ISNULL function

IFNULL function

Function: Determine whether a field or expression is null. If it is null, return the specified value, otherwise return the original value.

ISNULL function

Function: Determine whether a field or expression is null. If so, return 1, otherwise return 0.

Advanced 3. Sorting query

/*grammar:

select query list

from table

(where filter condition)

order by sorted list (asc (ascending order) | desc (descending order))

Features:

1. asc represents ascending order, and desc represents descending order. If not written, the default is ascending order.

2. The order by clause can support a single field, multiple fields, expressions, functions, and aliases.

3. The order by clause is generally placed at the end of the query statement, except for the limit clause.

*/

Case 1: Query employee information and request salary sorting from high to low

SELECT

    *

FROM

    employees

ORDER BY salary DESC;

search result:

Case 2: Query employee information with department number >= 90, sort by entry date [Add filter conditions]

SELECT

    *

FROM

    employees

WHERE

    department_id>=90

ORDER BY hiredate;

search result:

Case 3: Display employee information and annual salary from low to high [Sort by expression]

SELECT

    *,

    salary*12*(1+IFNULL(commission_pct,0)) AS 年薪

FROM

    employees

ORDER BY salary*12*(1+IFNULL(commission_pct,0)) ASC;

search result:

Case 4: Display employee information and annual salary from low to high [Sort by alias]

SELECT

    *,

    salary*12*(1+IFNULL(commission_pct,0)) AS 年薪

FROM

    employees

ORDER BY 年薪 ASC;

search result:

Case 5: Display the names and salaries of employees according to the length of their names [Sort by function]

SELECT

    LENGTH(last_name) 字节长度,

    last_name,

    salary

FROM

    employees

ORDER BY 字节长度 ASC;

search result:

Case 6: Query employee information, requiring ascending order first by salary, then descending order by employee number [sort by multiple fields]

SELECT

    *

FROM

    employees

ORDER BY salary ASC,employee_id DESC;

search result:

Advanced 4. Common functions

/*

Concept: Similar to Java methods, a set of logical statements are encapsulated in a method and the method name is exposed to the outside world.

Benefits: 1. Hidden implementation details; 2. Improved code reusability

Call: select function name (actual parameter list) [from table]

Features: 1. What is it called (function name); 2. What does it do (function function)

Category: 1. Single-line function. Such as concat, length, ifnull, etc.

2. Grouping function (function: used for statistics, also known as statistical function, composite function, group function)

*/

1. Character functions

1. length gets the number of bytes of the parameter value

2. concat concatenates strings

3、upper、lower

Case: Change the last name to uppercase, the first name to lowercase, and then splice them together

4. substr/substring Note: Index starts from 1

Extract all characters following the specified index

Extract characters of specified length from specified index

Case: Capitalize the first letter of the name, lowercase the other characters and then splice them with _ to display them

5. instr returns the index of the first occurrence of the substring. If it cannot be found, it returns 0.

6、trim

7. lpad uses specified characters to left pad the specified length

8. rpad uses the specified characters to right-fill the specified length

9. replace replace

2. Mathematical functions

1. round rounding

2. ceil rounds up and returns >= the smallest integer of the parameter

3. Floor rounds down and returns <= the maximum integer of the parameter.

4. Truncate and truncate, retaining several decimal places.

5. The remainder of mod has the same meaning as %. Calculation rule "MOD(a,b) is equivalent to aa/b*b"

3. Date function

1. now returns the current system date + time

2. curdate returns the current system date, excluding time

3. curtime returns the current time, excluding date

4. You can get the specified part, year, month, day, hour, minute, second

5. str_to_date converts characters in date format into dates in specified formats

6. date_format converts date into characters

4. Other functions

SELECT VERSION(); View version number

SELECT DATABASE(); View the current library

SELECT USER(); View current user

5. Process control function

1. if function is equivalent to the effect of if else

2. The use of case function is equivalent to the effect of Switch case

/*grammar

The field or expression to be judged by case

when constant 1 then value 1 or statement 1 to be displayed;

when constant 2 then value 2 or statement 2 to be displayed;

.........

else value n or statement n to be displayed;

end

*/

Case: Query employee's salary, requirements: department number = 30, the displayed salary is 1.1 times; department number = 40, the displayed salary is 1.2 times; department number = 50, the displayed salary is 1.3 times; for other departments, the displayed salary It’s the original salary

3. Use of case function 2 is similar to multiple if

/*

case

when condition 1 then value 1 or statement 1 to be displayed;

when condition 2 then value 2 or statement 2 to be displayed;

.......

else value n or statement n to be displayed;

end

*/

Case: Query the salary of employees. If the salary is >20,000, level A is displayed; if the salary is >15,000, level B is displayed; if salary is >10,000, level C is displayed; otherwise, level D is displayed.

2. Grouping function

/*

Function: used for statistics, also known as aggregate function or statistical function or group function

Category: sum sum; avg average; max maximum value; min minimum value; count calculation number

Features:

1. Sum and avg are generally used to deal with numerical types.

      max, min, count can handle any type

2. The above grouping functions ignore null values.

3. It can be used with distinct to implement deduplication operations.

*/

1. Simple to use

2. Detailed introduction of count function

Efficiency: Under the MYISAM storage engine, count(*) is highly efficient.

Under the INNODB storage engine, the efficiency of count(*) and count(1) is almost the same, which is higher than count(field).

3. There are restrictions on the fields that can be queried together with the grouping function.

The fields queried together with the grouping function must be the fields after group by

DATEDIFF function

  

Guess you like

Origin blog.csdn.net/weixin_45502414/article/details/123056482