SELECT CONCAT(id, name, age) as result_column from user; -- 12grig18
SELECT CONCAT_WS(',','1','2','3','4'); -- 1,2,3,4
group_concat(distinct concat_ws(':',date_format(start_time,'%Y-%m-%d'),tag) order by start_time separator ';') -- 2021-07-02:SQL;2021-07-05:SQL;2021-09-01:;2021-09-02:SQL;2021-09-05:SQL
SELECT MID(column_name,start[,length]) FROM table_name; -- start 1
left(string,length) -- left('abcd',3),abc
right(string,length) -- right('abcd',3),bcd
select * from table limit num -- num
select * from table limit start,num -- start0num
SELECT UPPER('Allah-hus-w3cschool'); -- ALLAH-HUS-W3CSCHOOL
select upper(column_name) from table_name;
--
SELECT UCASE(column_name) FROM table_name;
SELECT LOWER('W3CSCHOOL'); -- w3cschool
select lower(column_name) from table_name;
--
SELECT LCASE(column_name) FROM table_name;
SELECT TRIM(' Sample '); -- 'Sample'
SELECT LTRIM(' Sample '); -- 'Sample '
SELECT RTRIM(' Sample '); -- ' Sample'
update `article` set title=replace(title,'w3cschool','hello'); -- articletitlew3cschoolhello
SELECT LEN(column_name) FROM table_name;
-- decimals
SELECT ROUND(column_name,decimals) FROM table_name;
SELECT FORMAT(column_name,format) FROM table_name;
SELECT ProductName, Price, FORMAT(Now(),'YYYY-MM-DD') AS PerDate FROM Products;
select student_id,student_name,score,mod(score,3) as '' from student_score_info WHERE id = 8
-- 20210103 65.00 2.00
SELECT * FROM employee_tbl ORDER BY RAND(); --
-- RAND() RAND()
SELECT RAND(1) -- 1
-- group by使where使having
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
-- union
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
-- union all
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
-- 使
select
*
from
(
select
any_value (exam_id) as tid,
any_value (count(distinct uid)) as uv,
any_value (count(start_time)) as pv
from
exam_record
group by
exam_id
order by
uv desc,
pv desc
) a
union all
select
*
from
(
select
any_value (question_id) as tid,
any_value (count(distinct uid)) as uv,
any_value (count(submit_time)) as pv
from
practice_record
group by
question_id
order by
uv desc,
pv desc
) b
replace into examination_info values (null,9003,'SQL','hard',90,'2021-01-01 00:00:00 ');
--
delete from exam_record where adddate(start_time,interval 5 minute) > submit_time and score < 60 -- 5
delete from exam_record where adddate(start_time,interval 1 day) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 hour) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 second) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 microsecond) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 week) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 month) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 quarter) > submit_time and score < 60 -- 1
delete from exam_record where adddate(start_time,interval 1 year) > submit_time and score < 60 -- 1
--
select addtime('13:05','02:57') -- 16:02:00
select addtime('11:05','02:57') -- 14:02:00
--
select datediff('20191010','20191001') -- 9
--
select timediff('2019-06-03 12:30:00', '2019-06-03 12:29:30') -- 00:00:30
select timediff('12:30:00', '12:29:30') -- 00:00:30
create index idx_duration on examination_info (duration); --
create unique index uniq_idx_exam_id on examination_info (exam_id); --
create fulltext index full_idx_tag on examination_info (tag); --
--
SHOW INDEX FROM examination_info
--
ALTER TABLE table_name DROP INDEX index_name -- mysql
DROP INDEX table_name.index_name --sql server
-- charvarchartext使%xx
CREATE FULLTEXT INDEX index_info ON tb_student(info);
-- 使PRIMARY KEY
--
CREATE UNIQUE INDEX index_id ON tb_student(id);
--
CREATE INDEX index_id ON tb_student(id);
-- ,
CREATE INDEX index_na ON tb_student(C1,C2,C3);
-- WHERE c1 = x AND c2 = y AND c3 = z;
-- WHERE c1 = x AND c3 = z; C1C3C2C3
-- WHERE c1 > x AND c2 = y AND c3 = z; C1C1
-- WHERE c1 = x AND c2 < y AND c3 = z; C2C1C2
-- WHERE c1 = x AND c3 = z ORDER BY c2 ; C1C2C3
-- INNER JOIN JOIN INNER JOIN
SELECT column_name(s)
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name;
-- LEFT JOIN table1使table2
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
-- RIGHT JOIN table2使table1
SELECT column_name(s)
FROM table1
RIGHT JOIN table2 ON table1.column_name = table2.column_name;
-- FULL OUTER JOINtable1table2
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;
select ei.tag,ei.difficulty,format((sum(er.score)-max(er.score)-min(er.score))/(count(*)-2),1) from examination_info ei left join exam_record er on ei.exam_id = er.exam_id where ei.tag = 'SQL' and ei.difficulty='hard' and er.submit_time is not null
SELECT FORMAT(12562.6655,2); -- 12,562.67
SELECT FORMAT(12332.1,4); -- 12,332.1000
select truncate(4545.1366,2); -- 4545.13
select convert(4545.1366,decimal(10,2)); -- 4545.14
ROUND(748.58, -1); -- 750
ROUND(748.58, -2); -- 700
ROUND(748.58, -3); -- 1000
select ROUND(4545.1366,2); -- 4545.15
select
count(*) as total_pv, --
count(submit_time) as complete_exam_cnt, -- submit_timenull
count(distinct exam_id,submit_time is not null or NULL) --
from exam_record
select
score as min_score_over_avg
from
examination_info ei
left join exam_record er on ei.exam_id = er.exam_id
where
ei.tag = 'SQL'
and score >= (
select
avg(er.score)
from
examination_info ei
left join exam_record er on ei.exam_id = er.exam_id
where
ei.tag = 'SQL'
)
order by
score
limit
1
select
date_format(submit_time,'%Y%m') as submit_month,
count(question_id) as month_q_cnt,
any_value(round(count(question_id)/day(LAST_DAY(submit_time)),3)) as avg_day_q_cnt
from
practice_record
where
year(submit_time) = 2021
group by submit_month
union all
select
'2021' as submit_month,
count(*) as month_q_cnt, -- count(*)
round(count(1)/31,3) as avg_day_q_cnt -- count(1)
from
practice_record
where
year(submit_time) = 2021
order by submit_month

%Y 年,4 位,如:2017 %y 年,2 位,如:17 %M 月名,如:July %m 月名,数字,00-12 %D 带有英文前缀的月中的天:2nd %d 月的天,数值(00-31) %H 小时 (00-23) %h 小时 (01-12)


date_format(submit_time,'%Y%m') 获取年月,如:202307

year(submit_time) 获取年,如:2023

month(submit_time) 获取月,如:7

day(submit_time) 获取天,如:5

last_day(submit_time) 获取时期月份最后一天,如:2023-07-31

CASE WHEN SCORE = 'A' THEN ''
WHEN SCORE = 'B' THEN ''
WHEN SCORE = 'C' THEN '' ELSE '' END
select
device_id,
gender,
case
when age < 20 then '20'
when age between 20 and 24 then '20-24'
when age >= 25 then '25'
else ''
end as age_cut
from
user_profile

IF( expr1 , expr2 , expr3 )

expr1 的值为 TRUE,则返回值为 expr2 expr1 的值为FALSE,则返回值为 expr3

SELECT IF(TRUE,1+1,1+2);
-> 2
SELECT IF(FALSE,1+1,1+2);
-> 3
--
select
if (
age < 25
or age is null,
'25',
'25'
) as age_cut,
count(*) as number
from
user_profile
group by
age_cut
--
select
'25' as age_cut,
count(device_id) as number
from
user_profile
where
age < 25
or age is null
union
select
'25' as age_cut,
count(device_id) as number
from
user_profile
where
age >= 25;

IFNULL 表达式

IFNULL( expr1 , expr2 )

如果expr1不为空,直接返回expr1;

如果expr1为空,返回第二个参数 expr2

NULLIF 表达式

NULLIF(expr1,expr2)

如果两个参数相等则返回NULL,否则返回第一个参数的值expr1

视图

--
CREATE VIEW student_backup as
select student_name,student_id,course from student_score_info WHERE score>60
--
SELECT * FROM student_backup
--
DROP VIEW student_backup

插入查询结果

INSERT INTO db1_name (field1,field2) SELECT field1,field2 FROM db2_name
INSERT INTO xiaoshu_backup SELECT * FROM xiaoshu