mysql commonly used statement seven: query operations in DQL

1 Accumulation operation

The table structure is as follows

Insert picture description here

demand

Know the score, find the worst ranking. The results are sorted in ascending order of grade.

SQL statement:

select grade, sum(number) over(order by grade) as t_rank
from class_grade
order by grade;

Some might mysql version 不支持over()函数, 8.0more than just support.

2 Create a view

The table structure is as follows

Insert picture description here

demand

Created 视图actor_name_view, containing only first_nameand last_nametwo, and two columns renamed first_nameas first_name_v, last_namechanged to last_name_v.

SQL statement

create view actor_name_view as
select first_name as first_name_v, last_name as last_name_v from actor;

operation result

Insert picture description here

3 Right connection

The table structure is as follows

Insert picture description here
Insert picture description here

demand

Find all department employees have been assigned last_nameand first_nameas well dept_no, including temporary staff is not sector-specific allocation.

SQL statement

select e.last_name, e.first_name, d.dept_no from dept_emp as d
right join employees as e
on d.emp_no = e.emp_no;

# select e.last_name, e.first_name, d.dept_no from employees as e
# left join dept_emp as d
# on e.emp_no = d.emp_no;

This type of question 左连接may 右连接be used to get the correct result.

operation result

Insert picture description here

4 Left query, additional conditions

The table structure is as follows

Insert picture description here
Insert picture description here
Insert picture description here

demand

分类为nullThe movie idto be queried starts with 及名称.

SQL statement

select f.film_id, f.title from film as f
left join film_category as fc
on f.film_id = fc.film_id
where category_id is NULL;

Analysis, the joint query of the three tables is more complicated. The above statement can be divided into two parts.

select f.film_id, f.title from film as f
left join film_category as fc
on f.film_id = fc.film_id;

The running result of this part is
Insert picture description here
and then use the condition to filter out the desired records.

Of course, you can also joindirectly use the subquery without using the statement

select film_id, title from film
where film_id not in (select film_id from film_category);

Insert picture description here
It can be compared to another problem I recorded .

5 existskeywords

The table structure is as follows

Insert picture description here
Insert picture description here

demand

Find all the information of employees who are not assigned to a specific department

SQL statement

select * from employees
where not exists (select emp_no from dept_emp where employees.emp_no = dept_emp.emp_no);

Insert picture description here
Of course you can also use not ininstead

select * from employees where emp_no 
not in (select emp_no from dept_emp);

Insert picture description here

Guess you like

Origin blog.csdn.net/Awt_FuDongLai/article/details/114678646