2.4 DQL common function

I. Overview

Function: A similar procedure in java

Benefits: improve reusability and hide implementation details

Call: select function name (argument list);

Second, the one-way function

1, character function

concat :连接   concat(last_name,first_name);

substr : intercepting substring substr (str, start1) substr ( str, start1, length) from a start position Start

Upper : variable capital

Lower : to lower case

the replace : Replace

length : Get byte length

the TRIM : a space before and after the go

LPAD : left padding

RPAD : right padding

InStr : Gets the index of the first occurrence of the substring

2, Mathematical Functions

Ceil : improvement ToSei

round : rounding

MOD : modulo

Floor : rounding down

TRUNCATE : truncates

RAND : acquiring a random number between 0 and 1 return the fraction

 

3, date function

 

now : returns the current date + time

year : the return of

month The : Returns the month

Day : Day return

the DATE_FORMAT : Converts a date to a character

CURDATE : returns the current date

STR_TO_DATE : Convert a character to date

curtime : Returns the current time

hour : hour

minute : minutes

SECOND, : s

DATEDIFF : Returns the number of days between two dates

MONTHNAME : Returns the month in English form

 
 

4. Other functions

Version : The current version of the database server

database: the currently open database

user: The current user

password : ( 'character'): Returns the character code in the form of

md5: ( 'character'): Returns the encrypted form of the character md5

 

 

 

5, the flow control function

 

IF (conditional expression, expression 1, expression 2) : If the condition expression is established, the expression returns 1, otherwise it returns an expression 2

②case Case 1

case variables or expressions or fields

when the constant value 1 then 1

2 then a constant value when 2

...

else the value of n

end

 

③case Case 2

case

value 1 when condition 1 then

when condition 2 then the value 2

...

else the value of n

end

 

Third, the group function

1 classification

max : maximum

min : minimum value

sum: 和

avg: 平均值

count: 计算个数

 

2、特点

 

①语法

select max(字段) from 表名;
 

②支持的类型

sum和avg一般用于处理数值型
max、min、count可以处理任何数据类型
 

③以上分组函数都忽略null

④都可以搭配distinct使用,实现去重的统计

select sum(distinct 字段) from 表;

⑤count函数

count(字段):统计该字段非空值的个数
count(*):统计结果集的行数
案例:查询每个部门的员工个数
1 xx    10
2 dd    20
3 mm    20
4 aa    40
5 hh    40
 
count(1):统计结果集的行数
 
效率上:
MyISAM存储引擎,count(*)最高
InnoDB存储引擎,count(*)和count(1)效率>count(字段)

 

⑥ 和分组函数一同查询的字段,要求是group by后出现的字段

 
 
 
 
 
 
 
 

Guess you like

Origin www.cnblogs.com/huabro/p/12624196.html