mysql commonly used statement three: query operations in DQL

1 Sort operation

The table structure is as follows
Insert picture description here
Find all dept_no, and sort

select dept_no from dept_emp
order by dept_no;

Insert picture description here
逆序排序

select dept_no from dept_emp
order by dept_no desc;

Insert picture description here

2 Deduplication operation

The table structure is the same as above
Find all dept_no, sort and remove duplicates

select distinct dept_no from dept_emp
order by dept_no desc;

It should be noted that it distinctmust be placed in the front
Insert picture description here

3 Number of queries

The table structure is the same as above
Find all dept_norows that appear more than or equal to 2 times

select  count(dept_no) from dept_emp 
group by dept_no
having count(dept_no)>=2;

It should be noted that the group by dept_nostatement cannot be omitted.
Insert picture description here

4 multiple conditions

Table structure above
lookup employeestable all emp_noodd, and last_namenot Marythe employee information, and follow the hire_datereverse order

select * from employees
where emp_no %2 =1 
and last_name != 'Mary'
order by hire_date desc;

It should be noted that the keywords used in parallel are and;
if an !=error is reported, replace it with <>, which also means not equal.
Insert picture description here

5 The largest query

The table structure is as follows, asking for salary 第二多of employees emp_noand their corresponding salary salary.
Insert picture description here

select emp_no, salary from salaries
order by salary desc
# limit 1,1;
limit 1 offset 1;

limit a,bEquivalent to limit a offset b, the value range is [a, a+b).
Insert picture description here

Guess you like

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