mysql (DQL-Basic Query)

mysql (DQL-Basic Query)

DQL (Data Query Language ) data query language query is the most frequently used operation. You can query data from one table or query data from multiple tables.

1. Grammar:

select query list from table name;

2. Features:

(1) The query list can be: fields, constants, expressions, functions in the table

(2) The result of the query is a virtual table

3. Query result processing:

(1) Query the constant value SELECT 100;

(2) Query expression: select 100*98;

(3) Query function: select function; / such as version()

(4) Specific column query: select column1, column2 from table

(5) All column query: select * from table

(6) Exclude duplicate rows: select distinct column1,column2 from table

(7) Arithmetic operator: +-* /

(8) Function:

Similar to the method in java, a set of logic statements are defined in the database in advance and can be called directly

(1) Benefits:

i.1 Hidden implementation details now()

i.2 Improve code reusability

i.3 Call: select function name (list of actual parameters) [from table];

(2) Classification:

i.1 Single-line functions: such as concat, length, ifnull, etc.

i.2 Grouping function: for statistical use, also known as statistical function, aggregate function, group function

One-line function

(1) Character function

length(): Get the number of bytes of the parameter value

char_length() gets the number of characters in the parameter value

concat(str1,str2,...): concatenation string

upper()/lower(): change the string to uppercase/lowercase

substring(str,pos,length): The position of the intercepted string starts at 1

instr (str, the specified character): returns the index of the first occurrence of the substring, or 0 if not found

trim(str): remove the spaces or substrings before and after the string, trim (specify the substring from the string)

lpad (str, length, padding character): Use the specified character to achieve left padding and fill str to the specified length

rpad (str, length, padding character): Use the specified character to achieve right padding and fill str to the specified length

replace(str,old,new): replace, replace all substrings

-- 单行函数:会对每一行的记录进行操作
SELECT LENGTH(stu_name)FROM t_student 
-- CHAR LENGTH(stu_name)  字符为单位 
SELECT CHAR_LENGTH(stu_name) FROM t_student  -- 获取参数值的字符个数


SELECT   LENGTH(stu_name) FROM t_student  -- 获取参数值的字节个数

 
SELECT   CONCAT(stu_name,':',stu_phone) FROM t_student  -- 拼接字符串

SELECT   UPPER( stu_name) FROM t_student  -- 将字符串变成大写/小写
SELECT   LOWER( stu_name) FROM t_student  -- 将字符串变成大写/小写
 SELECT  SUBSTRING(stu_name,1,2) FROM t_student  -- 截取字符串   位置从1开始

 SELECT  INSTR(stu_name,'涛') FROM t_student  -- 返回子串第一次出现的索引,如果找不到返回0

 SELECT  TRIM(stu_name) FROM t_student  -- 去掉字符串前后的空格或子串,trim(指定子串 from 字符串)

  SELECT  LPAD(stu_name,1,'abd') FROM t_student   -- 用指定的字符实现左填充将str填充为指定长度

 SELECT  RPAD(stu_name,1,'123') FROM t_student    -- 用指定的字符实现右填充将str填充为指定长度

 SELECT  REPLACE(stu_name,'涛','华') FROM t_student   -- 替换,替换所有的子串

(2) Mathematical functions

round (number): rounding

ceil (number): round up, return >= the smallest integer of the parameter

floor (number): round down, return <= the largest integer of the parameter

truncate (number, keep the number of decimal places): truncated, truncated to a few places after the decimal point

mod (dividend, divisor): take the remainder, if the dividend is positive, it is positive; if the dividend is negative, it is negative

rand(): Get a random number and return a decimal between 0-1

(3) Date function

now(): return the current system date+time

curdate(): returns the current system date, excluding time

curtime(): returns the current time, excluding the date

You can get the specified part, year, month, day, hour, minute, second
YEAR (date), MONTH (date), DAY (date), HOUR (date), MINUTE (date) SECOND (date)

str_to_date: Convert the characters in the date format to the date in the specified format

date_format: Convert date to string

datediff(big,small): Returns the number of days between two dates

Date format

%AND Years, 4 digits
%m Month, number (00-12)
%d Day of month, numeric value (00-31)
%H Hour (00-23)
%i Minutes, number (00-59)
%s Seconds (00-59)
%f Microsecond
%T Time, 24-hours (hh:mm:ss)
%j Day of the year (001-366)
%w Day of the week (0=Sunday, 6=Saturday)

(4) Grouping function

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

Classification : sum sum, avg average, max maximum, min minimum, count (not empty)

(1).sum, avg are generally used to deal with numerical max, min, count can deal with any type

(2). The above grouping functions ignore null values

(3). It can be used with distinct to achieve de-duplication operation

(4). The general use of the count function count (*) is used to count the number of rows * All columns, when encountering a column that is empty, find the one that is not empty for statistics

(5.) The field requirement for query together with the grouping function is the field after group by

-- 统计函数,组函数,聚合函数
-- sum 求和、avg 平均值、max 最大值、min 最小值、count 计数(非空)

SELECT SUM(stu_score) FROM t_student

SELECT MIN(stu_score) FROM t_student

SELECT MAX(stu_score) FROM t_student

SELECT AVG(stu_score) FROM t_student

-- 列的值如果为null不会被统计,  一般使用主键或者*  *所有列,遇到一个为空的列,重新找不为空的进行统计
SELECT COUNT(stu_sex) FROM t_student


-- 分组查询  分组   会将相同内容分到同一个组   例如使用性别分组
-- 男 2
-- 女 2
-- select 结果  from 表 gronp by 分组列

-- 统计男生女生各有多少人
SELECT stu_sex,COUNT(*)  FROM t_student GROUP BY stu_sex
SELECT stu_sex,MAX(stu_score)  FROM t_student GROUP BY stu_sex


-- 添加查询条件

SELECT stu_sex,COUNT(*) c 
FROM t_student 
WHERE stu_score>0 -- 在分组前对数据进行筛选过滤
GROUP BY stu_sex
ORDER BY c DESC  -- 对分组后的结果进行排序
LIMIT 1

-- 查询性别人数大于2的  是哪个性别   对分组后的结果进行筛选过滤
SELECT stu_sex,COUNT(*) c 
FROM t_student 
WHERE stu_score>0 
GROUP BY stu_sex
HAVING c>=2      -- 对分组后的结果进行条件过滤
ORDER BY c DESC  

Guess you like

Origin blog.csdn.net/ZJ_1011487813/article/details/112992477