当前位置:网站首页>Mysql database (2)
Mysql database (2)
2022-07-08 01:38:00 【Si Xiaoyou】
Catalog
1. Table structure
create table student3(
id int, -- Number
name varchar(20), -- full name
age int, -- Age
sex varchar(5),-- Gender
address varchar(100),-- Address
math int,-- mathematics
english int -- English
);
insert into student3(id,NAME,age,sex,address,math,english) values(1,' Jack ma, ',55,' male ',' Hangzhou ',66,78),(2,' ma ',45,' Woman ',' Shenzhen ',98,87),(3,' Ma Jingtao ',55,' male ',' Hong Kong ',56,77),(4,' Liu Yan ',20,' Woman ',' hunan ',76,65),(5,' Liu Qing ',20,' male ',' hunan ',86,NULL),(6,' Lau Andy ',57,' male ',' Hong Kong ',99,99),(7,' Mard ',22,' Woman ',' Hong Kong ',99,99),(8,' De Marcia ',18,' male ',' nanjing ',56,65);
2. Query statement
-- Flexible query of all data Partial column data
-- select * from Table name ;
-- select Field name , Field name from Table name ;
select * from student3;
select id,name from student3;
-- Query statements can be conditional
-- select * from student3 where Conditions ;
-- Comparison operator > = >= <= != <>
-- between and
-- in stay .. Inside
-- like Fuzzy query
-- and and or perhaps not
-- Query math scores greater than 80 The students of grade
select * from student3 where math > 80;
-- Query math score less than 80 The students of grade
select * from student3 where math < 80;
-- Query English score less than 80 The students of grade
select * from student3 where english <= 80;
-- Query English score greater than 80 The students of grade
select * from student3 where english >= 80;
-- between and stay .. Section contain 65, contain 80, Baotou and Baowei
-- Query English scores in 65 and 80 What are the students between
-- and Both conditions must be met
select * from student3 where english >= 65 and english <=80;
select * from student3 where english between 65 and 80;
-- or and One condition is true
select * from student3 where english >= 80 or english <= 99;
-- not Not Don't set up
select * from student3 where not english >= 80;
-- in stay .. Inside
-- English scores in 80 branch ,90 branch english = 80 or english = 90
select * from student3 where english(80,90,70,77);
-- between and In the interval A range english>=65 and english <=80 65<=english<=80
-- in Equal to the value english = 80 or english = 90 or english = 70 english=80 english=90
select * from student3;
-- like Fuzzy query Match any number of characters Find out
-- % Match any number of characters _ Match a character
-- Inquire about Information of students surnamed ma Horse %
select * from student3 where name like ' Horse %';
-- The name contains Horse I can find out all the student information of the word
select * from student3 where name like '% Horse %';
-- Horse ending
select * from student3 where name like '% Horse ';
-- Inquire about Information of students surnamed ma Horse _
select * from student3 where name like ' Horse __';
select * from student3 where english is NULL;
3. Sort
-- order by Field name asc The default is asc From small to large Descending order from big to small
-- Sort in ascending order by age
select * from student3 order by age;
select * from student3 order by age desc;
-- In ascending order of age Is the descending order of math scores ok ?
select * from student3 order by age asc,math desc;
4. Aggregate functions
-- One by one Return the result according to the value of a column
-- max( Name ) Taking the maximum
-- min( Name ) Minimum value
-- avg( Name ) Average.
-- count( Name ) Count the data in this column
-- sum( Name ) Sum up
-- Query the oldest students A data
select * from student3;
select max(age) from student3;
select min(age) from student3;
select avg(math) from student3;
-- count( Name ) Not counted as null Of
select count(id) from student3;
select count(*) from student3;
select count(1) from student3;
select sum(math) from student3;
5. grouping
-- group by Will be used with aggregate functions
-- group by Will not be used alone , It doesn't make sense to use it alone
-- group by grouping
-- grouping Divide into two groups of men and women male and Woman Data only 2 That's ok
select * from student3 group by sex;
-- Count the number of men and women Statistics Take an alias as Use spaces
select count(sex) as ' Statistics ',sex ' Gender ' from student3 group by sex;
-- The average score of male and female English
select avg(english),sex from student3 group by sex;
-- The age of inquiry is 28 year ( contain ) More than students Group by sex
select count(sex) as ' Statistics ',sex from student3 where age>=28 group by sex;
-- The age of inquiry is 28 year ( contain ) More than students Group by sex , The number of inquiry gender is greater than 2 The data of
-- This condition can only be done after grouping
select count(sex) as ' Statistics ',sex from student3 where age>=28 group by sex having count(sex)>2;
-- where and having It's all conditional
-- where Is to filter data before grouping You can't add aggregate functions
-- having Is to filter data after grouping
6. Pagination
-- limit Limit the number of pieces of data on a page
-- limit Starting number from 0 Start How many pieces of data are displayed in total
-- Just want to show 3 Data Show 4,5,6 3,3
select * from student3 limit 1,3;
-- Just write a number
select * from student3 limit 3;
select * from student3 limit 3,3;
7. Link query
-- Foreign keys foreign key Primary key primary key
-- The employee table Number 、 full name 、 Age Primary key The number is unique and will not repeat The foreign key corresponds to the primary key of another surface The corresponding information can be found through foreign keys
-- Departmental table Number 、 name Primary key The number is unique and will not repeat
create table dept(
id int primary key auto_increment,
name varchar(20)
)charset=utf8;
create table emp (
id int primary key auto_increment,
name varchar(10),
gender char(1), -- Gender
salary double, -- Wages
join_date date, -- Date of entry
dept_id int,
foreign key (dept_id) references dept(id))charset=utf8; -- Foreign keys , Related department table ( The primary key of the Department table ) )
insert into dept values(1,' R & D department '),(2,' Testing department '),(3,' Operation and maintenance department '),(4,' The sales department ');
insert into emp(name,gender,salary,join_date,dept_id) values(' The Monkey King ',' male ',7200,'2013-02-24',1);
insert into emp(name,gender,salary,join_date,dept_id) values(' Pig eight quit ',' male ',3600,'2010-12-02',2);
insert into emp(name,gender,salary,join_date,dept_id) values(' Tang's monk ',' male ',9000,'2008-08-08',2);
insert into emp(name,gender,salary,join_date,dept_id) values(' Bones jing ',' Woman ',5000,'2015-10-07',3);
insert into emp(name,gender,salary,join_date,dept_id) values(' Spider essence ',' Woman ',4500,'2011-03-14',1);
select * from emp;
select * from dept;
-- The requirement is to query the employee information , Employee number , full name , Gender , Wages , Entry date and department name
-- from What watch With conditions
-- Implicit query
select emp.id,emp.name,gender,salary,join_date,dept.name from emp,dept where emp.dept_id = dept.id;
-- a surface inner join on b surface on Conditions
select emp.id,emp.name,gender,salary,join_date,dept.name from emp inner join dept on emp.dept_id = dept.id;
-- a surface left join b surface on Conditions Based on the left table , The data in the left table are all displayed .
select emp.id,emp.name,gender,salary,join_date,dept.name from emp left join dept on emp.dept_id = dept.id;
-- a surface right join b surface on Conditions
select emp.id,emp.name,gender,salary,join_date,dept.name from emp right join dept on emp.dept_id = dept.id;
边栏推荐
- Matlab code about cosine similarity
- Anaconda3 download address Tsinghua University open source software mirror station
- The combination of relay and led small night light realizes the control of small night light cycle on and off
- Redis master-slave replication
- Gnuradio operation error: error thread [thread per block [12]: < block OFDM_ cyclic_ prefixer(8)>]: Buffer too small
- Write a pure handwritten QT Hello World
- The persistence mode of redis - RDB and AOF persistence mechanisms
- Guojingxin center "APEC investment +": some things about the Internet sector today | observation on stabilizing strategic industrial funds
- 5. Contrôle discret et contrôle continu
- 2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
猜你喜欢
The persistence mode of redis - RDB and AOF persistence mechanisms
About snake equation (3)
Transportation, new infrastructure and smart highway
Probability distribution
Different methods for setting headers of different pages in word (the same for footer and page number)
The communication clock (electronic time-frequency or electronic time-frequency auxiliary device) writes something casually
2022 new examination questions for crane driver (limited to bridge crane) and question bank for crane driver (limited to bridge crane) operation examination
5、離散控制與連續控制
2022 chemical automation control instrument examination summary and chemical automation control instrument simulation examination questions
COMSOL - Construction of micro resistance beam model - final temperature distribution and deformation - establishment of geometric model
随机推荐
Mat file usage
Understanding of prior probability, posterior probability and Bayesian formula
Continued from the previous design
STM32GPIO口的工作原理
Kafka-connect将Kafka数据同步到Mysql
npm 内部拆分模块
Common operations of numpy on two-dimensional array
小金额炒股,在手机上开户安全吗?
Redis集群
QT -- package the program -- don't install qt- you can run it directly
Break algorithm --- map
Macro definition and multiple parameters
Kindle operation: transfer downloaded books and change book cover
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Basic realization of line chart (II)
Common fault analysis and Countermeasures of using MySQL in go language
2022 examination for safety production management personnel of hazardous chemical production units and new version of examination questions for safety production management personnel of hazardous chem
LaTeX 中 xcolor 颜色的用法
Working principle of stm32gpio port
How to get the first and last days of a given month