当前位置:网站首页>SQL review, grammar notes, fresh out
SQL review, grammar notes, fresh out
2022-04-22 07:25:00 【Small tomatoes that don't grow fat】
- left outer join , The left outer join ,outer Omission , In addition to matching rows in the result table , It also includes rows with mismatches in the left table and in the right table , For such a line , Select the column in the right table and set it to null .left join Used frequently in development !
- from clause on Conditions are mainly used to represent the same fields in the table , Other conditions that do not belong to the join table can use where Clause to specify .
- SQL When no sort method is specified , Default ascending order !
- The last day each user logged in ?

select user_id, max(date) as id from login
group by user_id;
- Support syntax : LIMIT # OFFSET #
SELECT * FROM table LIMIT 2 OFFSET 1;
OFFSET 1 Skip the first 1 Data ,LIMIT 2 Read 2 strip , The reading is the 2 strip , The first 3 strip .
SELECT * FROM table LIMIT 2,1;
Skip the former 2 Data , take 1 strip , Take the second 3 strip .
SELECT * FROM table LIMIT 0,5;
Take before 5 Pieces of data .
SELECT * FROM table LIMIT 2;
Before acquisition 2 Data .
- Keep the result after the decimal point 3 position (3 Round off the digits ):
round(avg(score)*1.0, 3)
- where Used before grouping , Cannot contain aggregate function .having Used after grouping , Filter the results after grouping . Yes having, There must be group by.
- The query in 2025-10-15 In the future, the status is successfully purchased C++ Course or Java Course or Python The order of , And in accordance with the order_info Of id Ascending sort .
select * from order_info
where date > '2025-10-15' and status='completed' and product_name in ('C++','Java','Python');
- The query in 2025 Posts and number of resumes delivered during the year , How to express 2025 During the year ?
10. year(date)=2025 //where Conditions
11. date between '2025-01-01' and '2025-12-31'
12. date >= '2025-01-01' and date <= '2025-12-31'
13. date like '2025%'
- count() and sum() The difference between

sum What counts is the sum :
select job, sum(num) as cnt
from resume_info
where date like '2025%'
group by job
order by cnt desc;
sum result :

count Count the number of times :
select job, count(num) as cnt
from resume_info
where date like '2025%'
group by job
order by cnt desc;
count result :

- Eliminate a data , Inquire about schools other than Fudan University
where university not in (' Fudan University ');
- There are three types that can be followed by aggregate functions
1、select
2、order by
3、having
appear having The reason is that ,where Keyword cannot be used with aggregate function ,having Keywords in group by After keyword , Filter the grouped data
- Three ways to filter null values :
1、where Name is not null
2、where Name != ‘null’
3、where Name != ‘’
- Modify the name of the table :alter table Table name rename 【to】 The new name of the table ;to Omission .
- take id=5 as well as emp_no=10001 Replace the row data with id=5 as well as emp_no=10005, Other data remain unchanged , Use replace Realization , Use it directly update Will report a mistake .

replace into titles_test
values ('5', '10005', 'Senior Engineer', '1986-06-26', '9999-01-01');
master replace into···values Usage of
replace into Follow insert into The function is similar to , The difference is :replace into First try inserting data into the table ,
If you find that this row of data already exists in the table ( Based on the primary key or unique index ) Then delete this row of data first , Then insert the new data ;
otherwise , Insert new data directly .
It should be noted that : The table that inserts data must have a primary key or a unique index ! Otherwise ,replace into Will insert data directly , This will result in duplicate data in the table .
- Be careful set There are multiple objects behind it ’,' instead of ’and’ Connect .
update titles_test set to_date=null, from_date='2001-01-01'
where to_date='2022-01-01';
- Delete emp_no Duplicate records , Keep only the smallest id Corresponding records .
delete from titles_test t where t.id not in
(select * from (select MIN(tt.id) from titles_test tt group by tt.emp_no) a);
pit :mysql Neutron query and delete The same table is not allowed , It is not allowed to check and delete , You need to alias the subquery .
- take employees Table for all employees last_name and first_name Spliced together as Name, Use a space in the middle to distinguish .
select concat(last_name," ", first_name) as Name from employees;
concat() Function is used to return the string after multiple string connections . If any parameter in the function is NULL, The return result is NULL. For string constants , You can also write them together directly .SELECT ‘MySQL’ ‘Java’ ‘ab’ AS str;
- The second highest paid employee emp_no And the corresponding salary salary,
If there are multiple employees, the salary is the second highest salary , The corresponding employee's emp_no and salary All output , And press emp_no Ascending sort .
SELECT emp_no, salary
FROM salaries
WHERE salary = (SELECT salary FROM salaries
GROUP BY salary
ORDER BY salary DESC LIMIT 1,1 );
- emp_no An odd number can be expressed as where emp_no%2 != 0
- Find all employees who are not department leaders emp_no.
select emp_no from employees
where emp_no not in (select emp_no from dept_manager);
- lookup employees All the information of the employee who ranks the third from the bottom in terms of time of entry into the company . Be careful : There may be employees on the same date , Therefore, there may be more than one employee who ranks the third from the bottom in terms of time of entry .
select * from employees
where hire_date = (select distinct(hire_date)
from employees
order by hire_date desc limit 2,1);
hire_date Third from the bottom , There may be more than one... In the last two , So remember to redo !
- on The condition is the condition used to generate the temporary table , It doesn't care on Is the condition in true , Will return to the records in the table on the left .where The condition is that after the temporary table is generated , Then filter the temporary table .
- Three tables are related .
select e.last_name, e.first_name, d.dept_name from employees e
left join dept_emp de on e.emp_no=de.emp_no
left join departments d on d.dept_no=de.dept_no;
- If not, insert , Ignore... If it exists .
INSERT IGNORE INTO tablename VALUES(...);
If not, insert , If it exists, replace .
INSERT REPLACE INTO tablename VALUES(...);
The existence here means unique When the column value of the attribute exists ,unique Indicates that the key value is unique .
30.

create table if not exists actor_name (
first_name varchar(45) not null comment ' name ',
last_name varchar(45) not null comment ' surname '
);
insert into actor_name select first_name,last_name from actor;
- Add unique index .
alter table actor add unique index uniq_idx_firstname(first_name);
Add a normal index .
alter table actor add index idx_lastname(last_name);
Add primary key index .
alter table actor add primary key index idx_lastname(last_name);
- in the light of salaries surface emp_no Field creation index idx_emp_no, Inquire about emp_no by 10005, Use force index .
select * from salaries FORCE INDEX (idx_emp_no) where emp_no = 10005;
- Add columns ,alter table Table name add Name type ;( By default, it is added to the last column of the table )
alter table actor add create_date datetime NOT NULL default '2020-10-01 00:00:00';
版权声明
本文为[Small tomatoes that don't grow fat]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220612065743.html
边栏推荐
- Error: (vlog-2892) Net type of 'i_yc422' must be explicitly declared.
- 【数论】素数(五):梅森素数(Lucas_Lehmer判定法)
- 面试官常问的,对象分配的一般过程及特殊情况
- Cannot find interface mapping after updating HDF
- Change the class with parameters in the first part of the last operation to be presented in the way without parameters, and the function remains unchanged.
- 二叉树链式结构操作LeetCode+牛客(详解)
- 【数论】素数(三):素数判断法(朴素法、模6法、Rabin-Miller及改进)
- [Opt 31-67] Problem axi_interconnect RTL报错
- Raspberry Pi 4b
- Unknown graphics extension:. 1 jpg. }
猜你喜欢

SQL server stored procedure development notes - piecemeal problems and operations on operation files

LaTex用模板的时候图片的caption标题无法左对齐

八大排序的思想及其代码

双向循环链表(详)

Process of stepping on the pit in the flutter environment

C语言 | 指针

Jenkins deployment PM2

快排与归并排序

. net learning notes (I) -- introduction, advantages, design ideas, principles and applications of generics

modelsim仿真加速注意点
随机推荐
idea 不显示Run Dashboard视图窗口的问题
sql server快速入门
SQL Server quick start
【题解】洛谷P6186 [NOI Online #1 提高组] 冒泡排序:【冒泡排序】与【逆序对】问题
synchronized锁优化的一些机制(锁升级)
【数论】同余(四):一元线性同余方程组(两两相消、中国剩余定理)
Quartus II prevents signals from being integrated
[DRC 23-20] Rule violation (REQP-1712) Input clock driver - Unsupported PLLE2_ADV connectivity.
桥接模式下主机ping不通虚拟机
Detailed tree array template -- Theory and code implementation
[number theory] congruence (7): fast power, fast power of matrix
队列(详解)——手撕队列习题
虚拟机磁盘空间缩小
JVM中的逃逸分析,可以实现不在堆上分配内存
What is the internal structure of stack frame?
Prompt the user to enter his name, and the user will write his name to the file guest Txt program determines that when it is not equal to N, it executes to create the file data Txt, a total of 100000
1. Compile the following three modules of student information management system: and detect the implementation. 1. Add student information 4. Query student information 5. Query all student information
What is socket programming?
【数论】同余(二):逆元
[number theory] prime number (I): basic concepts, properties, conjectures and theorems