当前位置:网站首页>SQL learning notes (04) - data update and query operations
SQL learning notes (04) - data update and query operations
2022-07-01 09:26:00 【Yuxuan godlike】
The contents of the catalog
One 、 Data update operation
1. Add data (insert Insert statement )
The inserted values and columns should maintain a one-to-one correspondence
-- Mode one : Use multiple insert Insert statement
-- insert into Table name ( Name 1, Name 2, Name 3.....) value( value 1, value 2, value 3...);
insert into students (id,name) values (0,' Zhang Fei ');
insert into students (id,name) values (1,' zhaoyun ');
-- Mode two : Pass one insert Insert multiple data
-- insert into Table name value( value 1, value 2, value 3...);
insert into heros value(0,' Sun shangxiang ',' Woman ',20),(1,' Luna ',' Woman ',18);
-- Mode three :insert into Table name ( Name 1, Name 2, Name 3.....) value( value 1, value 2, value 3...),( value 1, value 2, value 3...);
insert into heros(id,name,sex,age) value(0,' Sun shangxiang ',' Woman ',20),(1,' Luna ',' Woman ',18);
2. Delete data (delete Delete statements )
delete and drop The difference between :delete Only delete the data in the table , The watch is still there ;drop Is to delete the entire table
2-1. Physical delete
-- Format 1 : Delete the entire data in the table
-- delete from Table name ;
delet from students;
-- Format two : Delete data according to conditions
delete from Table name where Conditions ;
and The conditions are met at the same time delete from Table name where Conditions 1 and Conditions 2 and Conditions 3....
or Multiple conditions only need to meet one of them delete from Table name where Conditions 1 or Conditions 2 or Conditions 3....
2-2. Logical deletion
It refers to setting a field to identify that the current record has been deleted is_delete Field to identify ,1 Said to delete ,0 Indicates not deleted
update students set is_delete=1 where id=8;
2-3. Other ways to delete data
-- truncate table Table name ( Clear all the data in the table , But the table structure will be preserved , The value of the self increasing field will change from 1 Start .)
truncate tablestudens;
-- drop table Table name ( Delete data table , Including data and table structure )
drop table students;
3. Modifying data
-- Format 1 :
update Table name set Name = value ;
-- Format two :
update Table name set Name 1= value 1, Name 2= value 2...;
-- Format three : Conditional updates
update Table name set Name = value where Conditions ;
update The employee table set Wages =12000 where full name = Xiao Zhang ";
Two 、 Data query operation
1. Query basic syntax structure
- Query the values of some fields
select Field name 1, Field name 2 .... from Table name ( Query the information of a part of the field )
-- Look up the names in the student table 、 Gender 、 Age data
select name,sex,age from students;
- Take the alias (as keyword )
-- Aliasing tables
-- select Alias . Field name 1, Alias . Field name 2 ..... from Table name as Alias
select s.name,s.sex,s.age from students as s;
-- Alias field
-- select Field name 1 as Alias 1, Field name 2 as Alias 2 .... from Table name ;
select name as full name , sex as Gender ,age as Age from students;
- duplicate removal (distinct keyword )
select distinct Field name 1, Field name 2 .... from Table name ;
-- According to the age of the students
select distinct age from students;
2. Conditions of the query
2-1. Logical operators
- and( At the same time meet the conditions ),or( One of the conditions ),not( Does not meet this condition )
-- Grammatical structure
select Name from Table name where Conditions 1 Logical operators Conditions 2 Logical operators Conditions 3....;
-- Example
-- 1. Check the weather in Shanghai or Beijing
select * from weather where city=' Shanghai ' or city=' Beijing ';
-- 2. Check the weather except Shanghai
select * from weather where not city=' Shanghai ';
select * from weather where city!=' Shanghai ';
2-2. Comparison operator
- Comparison operator : Greater than (>)、 be equal to (=)、 Less than (<)、 Greater than or equal to (>=)、 Less than or equal to (<=)、 It's not equal to (<> perhaps !=)
-- 1. The query age is less than 18 Year old girl
select * from students where age < 18;
-- 2. The query result is greater than or equal to 85 Students with scores above
select * from students where grade >= 85;
-- 3. Query the names of all girls of gender
select * from students where sex <> ' male ';
select * from students where sex != ' male ';
2-3. Range queries
- between …and…: The data that meets a certain range of query results is within a certain range
select Name from Table name where Name between Starting value and End value ;
-- Query score in 85-90 Between the students
select * from students where grade between 85 and 90;
select * from students where grade >= 85 and grade <= 90;
- not between … and…: Data that is not within a certain range
select Name from Table name where Name not between Starting value and End value ;
-- The query score is not 85-90 Between the students
select * from students where grade not between 85 and 90;
- in: Query data in a discontinuous range ,
As long as one of them is satisfied, there will be query results
-- Check the weather in Shenzhen, Guangzhou or Shanghai
select * from weather where city in(' Shenzhen ',' Guangzhou ',' Shanghai ');
select * from weather where city = ' Shenzhen ' or city = ' Guangzhou ' or city = ' Shanghai ';
2-4. Fuzzy query
- Fuzzy query :like keyword
- % It stands for
Zero or morecharacter - _ It stands for
A character
-- 1. Look up the student surnamed Wang
select * from students where name like ' king %';
-- 2. Look up the student whose last name is Wang and whose first name is
select * from students where name like ' king _';
-- 3. The query name contains ' rain ' Of the students
select * from students where name like '% rain %';
-- 4. Query students whose names are two words
select * from students where name like '__';
-- 5. The inquiry number is in 5 The students at the end
select * from students where name like '%5';
2-5. Null value query
null and '' Empty characters are different
-- The judgment is empty :select Name from Table name where Name is null;
-- Query the information that the student subject is empty
select * from students where subject is null;
-- Judgment is not empty :select Name from Table name where Name is not null;
-- Query the information that the student subject is not empty
select * from students where subject is not null;
Sort (order by)
- The default is ascending ( From small to large )
- asc Arrange... In ascending order ( From small to large )
- desc Arrange... In descending order ( From big to small )
-- Format :select Name from Table name order by Name 1 asc, Name 2 desc;
-- Example ∶ Check the records of all students in the grade sheet , It is required to be arranged in ascending order of scores , The same results , Arrange in descending order according to the student number
select * from League tables order by achievement asc, Student number desc;
Grouping and aggregation functions
Aggregate functions
- sum( Name ) Sum a column
- avg ( Name ) Average a column
- max( Name ) Maximize a column
- min ( Name ) Find the minimum value for a column
- count(*) Count the number of tuples ( See how many records there are in a table )
- count( Name ) Count the number of values in a column
except count(*) Outside , When other functions operate again , Ignore null values (null)
-- Inquire about '1 class ' The sum of the students' scores
select sum(grade) from students where class='1 class ';
-- Inquire about '1 class ' The student's average score
select avg(grade) as average from students where class='1 class ';
-- Inquire about '1 class ' Students get the highest scores
select max(grade) as The highest from students where class='1 class ';
-- Inquire about '1 class ' The minimum age of students
select min(age) as Minimum age from students where class='1 class ';
-- Statistics name How many numbers are there
select count(name) from students;
-- Count the number of tuples
select count(*) from students;
Group query
- Group the query results
Format :select Name , Aggregate functions from Table name group by Name ;
-- Check the number of people of different genders
select sex,count(*) from students group by sex;
-- Check the number of students in each class
select class,count(*) from students group by class;
-- Query the number of people of different genders in each class
select class,sex,count(*) from students group by class,sex;
- Data filtering after grouping
- Treat the grouped data as a table data , And then through having Condition to filter the current table data .
- select Name from Table name group by Name having Conditions ( In general, aggregate functions are used as conditions );
Be careful :1.having It has to be with group by fixed collocation ;
2.having You can write aggregate functions later , however where You can't write .
-- Check the total number of girls
select count(*) from students where sex=' Woman ';
select sex,count(*) from students group by sex having sex=' Woman ';
-- Query the total number of girls in each class
select class,sex,count(*) from students group by class,sex having sex=' Woman ';
-- Query that the number of records of different genders in all classes is greater than 1 Information about
select class,sex,count(*) from students group by class,sex having count(*)>1;
Paging query
From the database 0 Start index
select Name from Table name limit [ Specify the row to start the query ,] Total rows queried ;
-- for example : Query the student ID in the student form from 3 OK, let's start , Inquire about 7 That's ok
select sno from students limit 2,7;
边栏推荐
- LeetCode 344. Reverse string
- Short circuit operator lazy evaluation
- The fixed assets management system enables enterprises to dynamically master assets
- 2.2 【pytorch】torchvision. transforms
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
- delete和delete[]引发的问题
- 集成积木报表报错 org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
- js函数arguments对象
- Phishing identification app
- JS prototype chain
猜你喜欢

Daily practice of C language - day 80: currency change

树结构---二叉树2非递归遍历

MapReduce programming basics

Mise en œuvre simple de l'équilibrage de la charge par nacos

JS原型链

【电赛训练】红外光通信装置 2013年电赛真题

Preparing for the Blue Bridge Cup -- bit operation

2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder

【pytorch】2.4 卷积函数 nn.conv2d

Design and manufacture of simple digital display electronic scale
随机推荐
Wechat applet WebView prohibits page scrolling without affecting the implementation of overflow scrolling in the business
【pytorch】nn. Crossentropyloss() and nn NLLLoss()
js valueOf 与 toString 区别
Class loading
A 419 error occurred in the laravel postman submission form. July 6th, 2020 diary.
R language observation log (part24) -- initialization settings
tensorrt yolov5_ trt. Py comments
In the middle of the year, where should fixed asset management go?
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
Vsync+ triple cache mechanism +choreographer
序列化、监听、自定义注解
js变量提升(hoisting)
ES6-const本质与完全不可改实现(Object.freeze)
The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
闭包实现迭代器效果
How to realize the usage of connecting multiple databases in idel
Understand shallow replication and deep replication through code examples
Principles of Microcomputer - Introduction
SQL学习笔记(04)——数据更新、查询操作
js this丢失问题分析 及 解决方案