第一题讲解
分析:
(分析要查的表):
(显示的列):
(关联条件):
(过滤条件):
[分组条件]:
[排序条件]:
[分页条件]:
SELECT
d.deptno, dname, loc, count(empno)
FROM dept d JOIN emp e
ON d.deptno = e.deptno
GROUP BY d.deptno;

自连接查询

create table area(
pid int primary key auto_increment,
name varchar(20),
city_id int
);
insert into area values (1,'广东省',null),(2,'江西省',null),(3,'广州市',1),(4,'深圳',1),
(5,'东莞',1),(6,'南昌',2),(7,'赣州',2),(8,'九江',2);
select province.pid,province.name,city.name,city.pid from area province join area city on province.pid = city.city_id;
select a.pid,a.name,b.name,b.pid from area a join area b on a.pid = b.city_id where a.name = '广东省';
子查询的三种情况
1.子查询当条件(单值对比)
2.子查询当条件(多值对比)
3.子查询当临时表(出现在from的后边位置)
select * from emp where sal > (select avg(sal) from emp);
select * from emp where deptno in (select deptno from dept where dname = '销售部' or dname='财务部');
select * from emp where deptno in (select deptno from dept where dname in ('销售部','财务部'));
select e.* from emp e join dept d on e.deptno = d.deptno where dname = '销售部' or dname='财务部';
select * from emp where sal > 15000;
select d.dname,d.loc,e.* from dept d join (select * from emp where sal > 15000) e on d.deptno = e.deptno;
union连接查询
union: 纵向拼接去重
union all:纵向拼接不去重
select * from emp where sal > 28000
union
select * from emp where deptno = 10;
select * from emp where sal > 28000
union all
select * from emp where deptno = 10;
create table teacher(
t_id int,
t_name varchar(20),
t_gender varchar(20)
);
create table student(
s_id int,
s_name varchar(20),
s_gender varchar(20)
);
insert into teacher values (1,'张三','男'),(2,'李四','男'),(3,'王五','男'),(4,'小美','女');
insert into student values (1,'tom','男'),(2,'jerry','男'),(3,'jack','男'),(4,'rose','女');
select t_id id,t_name name,t_gender gender from teacher where t_gender = '男'
union
select * from student where s_gender = '男';
数学函数
select round(3.1415926);
select round(3.1415926,2);
select round(3.145,2);
select format(12345.1415926,3);
select floor(12345.141592);
select floor(12345.541592);
select floor(12345.941592);
select ceil(12345.141592);
select ceil(12345.541592);
select ceil(12345.941592);
select mod(5,3);
select pow(2,3);
select rand();
select rand(10);
字符串函数
select upper('hello');
select lower('HELLO');
select s_id,upper(student.s_name),s_gender from student;
select replace('黑马程序员','黑马','白马');
select concat('a','黑','b','白',100,false,true);
select concat_ws('_','a','b','中文');
select repeat('我错了',3);
select reverse('abc');
select reverse('你好吗');
select substr('hello',2);
select substr('hello',1,1);
select substr('hello',-2,2);
select substr('hello',-5,5);
select left('hello',3);
select right('hello',3);
select char_length('abc');
select char_length('你好');
select length('abc');
select * from student;
select concat(upper(substr(student.s_name,1,1)),substr(s_name,2)) from student;
时间日期函数
select now();
select current_date();
select current_time();
select datediff('2023-09-18','2022-09-10');
select date_add(now(),INTERVAL 1 DAY);
select date_add(now(),INTERVAL -1 DAY);
select date_add(now(),INTERVAL 10 YEAR);
select date_sub('2000-10-11',interval 1 day);
select date_sub(20001010,interval 1 day);
select timestamp(now(),'10:10:10');
select YEAR(now());
select MONTH(now());
select day(now());
select hour(now());
select date_format(now(),'%Y年%m月%d日 %H:%i:%s');
select date_format(now(),'%Y-%m-%d %H:%i:%s');
select date_format(now(),'%Y/%m/%d %H:%i:%s');
select unix_timestamp();
select unix_timestamp(now());
select unix_timestamp('1970-01-01');
select from_unixtime(0);
select from_unixtime(1727252256);
select '2020-10-10' > '2020-10-09';
时间日期案例
Create table If Not Exists Logins (user_id int, time_stamp datetime);
Truncate table Logins;
insert into Logins (user_id, time_stamp) values ('6', '2020-06-30 15:06:07');
insert into Logins (user_id, time_stamp) values ('6', '2021-04-21 14:06:06');
insert into Logins (user_id, time_stamp) values ('6', '2019-03-07 00:18:15');
insert into Logins (user_id, time_stamp) values ('8', '2020-02-01 05:10:53');
insert into Logins (user_id, time_stamp) values ('8', '2020-12-30 00:46:50');
insert into Logins (user_id, time_stamp) values ('2', '2020-01-16 02:49:50');
insert into Logins (user_id, time_stamp) values ('2', '2019-08-25 07:59:08');
insert into Logins (user_id, time_stamp) values ('14', '2019-07-14 09:00:00');
insert into Logins (user_id, time_stamp) values ('14', '2021-01-06 11:59:59');
select user_id,max(time_stamp) from logins where year(time_stamp) = 2020 group by user_id;