基本语法:
最基本的查询语句是由 SELECT 和 FROM 关键字组成的
SELECT *FROM t_student;
SELECT id , name , sex FROM t_student;
列表取别名(力扣1148):
SELECT name, id AS student_id FROM t_student;
排序(order by 字段名 desc(acs))(desc为降序,acs为升序(默认为升序,即只写order by 字段名))(力扣1148)
SELECT name, id FROM t_student ORDER BY id DESC
取出重复项(SELECT DISTINCT 字段 FROM …… ;)(力扣1148)
SELECT DISTINCT author_id as id FROM Views
还可以插入到聚合函数中使用,例如力扣2356题
代码:
# Write your MySQL query statement below
select teacher_id,count(distinct subject_id) as cnt
from Teacher
group by teacher_id;
获取字符串长度(length(字段名))
SELECT length(name) FROM t_student
连接操作:
内连接:
表结构
fruit
Id:int
Name:varchar
Price
Id:int
Price:double
SELECT a.price ,b.name
FROM fruit b
JOIN t_price a
ON b.id=a.id
效果:只返回两表一一对应的结果,若不存在b.id=a.id则该项不返回
左外连接:
表结构
fruit
Id:int
Name:varchar
Price
Id:int
Price:double
SELECT a.price ,b.name
FROM fruit b
LEFT (OUTER) JOIN t_price a
ON b.id=a.id
效果:左表(fruit b)所有需要的数据保留,如果连接右表(t_price a)找不到对应数据,则该字段返回值为空(outer可加可不加),注:如果需要对连接后的数据进行筛选处理,可以在on的后面加入where来进行判断筛选
例题:力扣557
select e.name,b.bonus
from Employee e
left join Bonus b
on e.empId = b.empId
where b.bonus <1000 or b.bonus is null
交叉连接:
一般用来返回连接表的笛卡尔积,交叉连接可以查询两个或两个以上的表。
格式:SELECT <字段名> FROM <表1> CROSS JOIN <表2> [WHERE子句]
例题:力扣1280
select a.student_id,a.student_name,a.subject_name,ifnull(b.attended_exams,0) as attended_exams
from (select * from Students cross join Subjects) as a
left join (select student_id,subject_name,count(subject_name) as attended_exams
from Examinations
group by student_id,subject_name) as b
on a.student_id=b.student_id and a.subject_name=b.subject_name
order by a.student_id,a.subject_name asc;
右外连接:
表结构
fruit
Id:int
Name:varchar
Price
Id:int
Price:double
SELECT a.price ,b.name
FROM fruit b
RIGHT (OUTER) JOIN t_price a
ON b.id=a.id
效果:右表(a)的数据所需字段全部保留下来,如果左表(b)找不到一一对应的数据,则该表所需数据为空
a表左外连接b表 等价于 b表右外连接a表
dateDiff函数:
作用:前日期减去后面的日期
格式:Mysql:datediff(date1,date2) (时间单位为天)
例子:力扣197
select a.id
from Weather a,Weather b
where a.Temperature>b.Temperature and dateDiff(a.recordDate,b.recordDate) = 1 ;
分组查询(group by)
group by的常规用法是配合聚合函数,利用分组信息进行统计,常见的是配合max等聚合函数筛选数据后分析,以及配合having进行筛选后过滤。
聚合函数:
1.sum()求和
select sum(age)from 库名
2.count ()统计有多少条记录
select count(*)from 库名
3.avg() 求平均值
select avg(age)from 库名
4. max ()求最大值
select max(age)from 库名
5.min ()求最小值
select min (age) from 库名
例题:力扣1661
做法1:
select a.machine_id,round(avg(b.timestamp-a.timestamp),3) as processing_time
from Activity a
join Activity b
on a.machine_id=b.machine_id and a.process_id=b.process_id and a.activity_type='start' and b.activity_type='end'
group by a.machine_id;
补充:round()函数:保留几位小数(四舍五入)
DATE_FORMAT函数:
格式:DATE_FORMAT(date, format),。
作用:用于以不同的格式显示日期/时间数据。date 参数是合法的日期,format 规定日期/时间的输出格式。
例题:力扣1193
代码:
# Write your MySQL query statement below
select DATE_FORMAT(trans_date, '%Y-%m') as month,
country,
count(*) as trans_count,
count(if(state = 'approved',1,NULL)) as approved_count,
sum(amount) as trans_total_amount,
sum(if(state = 'approved',amount,0)) as approved_total_amount
from Transactions
group by month,country