当前位置:网站首页>MySQL basic query
MySQL basic query
2022-06-13 05:13:00 【strive_ one】
Mysql The basic query
- Create table
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2), -- Maximum 5 Digit number , Two decimal places
gender enum(' male ',' Woman ',' Simon? ',' A secret '),
cls_id int unsigned default 0,
isdelete bit default 0
);
- Insert data for the table :
insert into students values
(0,' Wei Shao ',18,180.00,2,1,0),
(0,' Little moon ',18,180.00,2,2,1),
(0,' peng ',29,185.00,1,1,0),
(0,' Lau Andy ',59,175.00,1,2,1),
(0,' lotus ',38,160.00,2,1,0),
(0,' feng ',28,150.00,4,2,1),
(0,' Joey wong ',18,172.00,2,1,1),
(0,' Jay Chou ',36,NULL,1,1,0),
(0,' Cheng Kun ',27,181.00,1,2,0),
(0,' Liu Yifei ',25,166.00,2,2,0),
(0,' Venus ',33,162.00,3,3,1),
(0,' Jingxiang ',12,180.00,2,4,0),
(0,' Zhou Jie ',34,176.00,2,5,0);
Query all the field information in the table :
select * from students;
Query the information of some fields in the table :
select name,age from students;
# perhaps
select students.name,students.age from students;
Give the table an alias
- Be careful : The previous ones also need to be modified , Otherwise, it will report a mistake
select s.name,s.age from students as s;
Alias fields
select s.name as name ,s.age as Age from students as s;
duplicate removal
select distinct id,gender from students;
mysql Conditions of the query
see mysql Version of
select version();
mysql Can perform basic operations
select 2 + 3;
where Query conditions
- where A variety of operators are supported , To deal with the conditions
- Comparison operator 、 Logical operators 、 Fuzzy query 、 Range queries 、 Empty judgment
- Comparison operator
- be equal to : =
- Greater than : >
- Greater than or equal to : >=
- Less than : <
- Less than or equal to : <=
- It's not equal to : != or <>
- such as :
select * from students where id = 10;
- Logical operators
- and
- When both sides of this symbol are true , The result is true , That is, the conditions are met .
- or
- When one of the two sides of the symbol is true , That's true , That is, the conditions are met .
- not
- such as :
select * from students where id > 3 and gender = 2;
select * from students where id < 4 or isdelete = 0;
select * from students where not id = 10;
Fuzzy query like
- % Represents any number of arbitrary characters
- a%b ==> aab, axxb, auuub, ab,
- _ Represents an arbitrary character
- a_b ==> acb, aub, aib,
- such as :
select * from students where name like ' Zhou %';
select * from students where name like ' Zhou _';
Range queries :
- in
- Represent in a discontinuous set :
- between … and …
- In a continuous range
select * from students where id in(1,3,8);
select * from students where id between 1 and 5;
Empty judgment
- Be careful :null And ’' Is different
- Sentenced to empty is null
- Cannot pass comparison operator = And null Compare , Need to use is null
- The verdict is not empty is not null
- such as
# Look up the students who didn't fill in the height
select * from students where height is null;
# Look up the students who have filled in their height
select * from students where height is not null;
priority
- The order of priority from high to low is : parentheses ,not, Comparison operator , Logical operators
- and Than or Calculate first , If it appears at the same time and wants to calculate first or, Need to combine () Use
Sort
- asc
- Sort from small to large , In ascending order ;
- desc
- Sort from large to small , In descending order ;
- The default is ascending ;
select * from students order by height desc;
# First in descending order of height , In descending order by age ;
select * from students order by height desc,age desc;
# Yes id yes 1 To 5 To sort the height
select * from students where id > 1 and id < 5 order by height desc;
Aggregate functions
count(*)
- To calculate the total number of lines :
select count(*) from students;
select count(height) from students;
max( Column )
- To find the maximum value of this column
select max(height) from students;
min( Column )
- To find the minimum value of this column
select min(height) from students;
sum( Column )
- Means to find the sum of this column
select sum(height) from students;
select sum(height) / count(id) from students;
avg( Column )
- Find the average of this column
mysql> select avg(height) from students;
grouping
- Group by fields : The same data in this field will be put into a group , After grouping , The column by which you group is displayed in the result set , Other columns do not appear in the result set , You can make statistics on the grouped data , Do aggregate operations .
- The purpose of grouping is to make statistics .
- By default : After grouping, the grouped data will be executed once Ascending Sorting operations
select gender from students group by gender;
# It can also be used. distinct duplicate removal , The effect is the same
select distinct gender from students;
# When sorting grouped data, you do not need to use order by;
select gender from students group by gender desc;
# Look at the groups by sex , Names of members of the group :
# No matter how many names there are , Can only see the first name
select gender,any_value(name) from students group by gender;
# And group_concat() Functions together , You can see all the names
select gender,group_concat(name) from students group by gender;
# You can look at your name and age at the same time
select gender,group_concat(name,age) from students group by gender;
# View the number of people of all ages
select age,count(*) from students group by age;
Data filtering after grouping
- having The following conditional operators and where identical
select count(*) from students where gender = 1;
# Or you could write it as
select count(*) from students group by gender having gender = 1;
select gender,count(*) from students group by gender having gender = 2;
select gender,count(*) from students where gender = 2 group by gender;
- contrast where And having
- where It's right from The table specified later performs data filtering , It belongs to the screening of original data
- having It's right group by Results of
- summary : Yes group by The appearance of a statement does not necessarily have having Occurrence of statement , Yes having When a statement appears, there must be group by Occurrence of statement .
use limit Limit the number of query results
- When querying data , You may find a lot of records . And the records that users need may be just a few . So you need to limit the number of query results .Limit yes mysql A special keyword in . It can be used to specify the record from which query results are displayed , You can also specify how many records are displayed . There are two uses :
- Do not specify initial position
- When the initial position is not specified , The record is displayed from the first record . The number of records displayed is limit Keyword assignment .
- If the number you specify is larger than the number of records in the actual table , Then the query result will display all the records of the table
select * from students order by height desc limit 3;
- Specify the initial position
- Specify which record to display from , And you can specify how many records to display
# When inquiring , The subscript from the record is 3 Start checking at this position ( The subscript is from 0 Begin to count ), Total enquiries 3 Bar record . Ahead 3 representative , Which record to start with , hinder 3 representative , Query several records in total , They are separated by commas .
select * from students order by height desc limit 3,3;
边栏推荐
- C language learning log 1.22
- Metartc4.0 integrated ffmpeg compilation
- Advanced C language - Section 1 - data storage
- Spread your wings and soar
- 【线程/多线程】线程的执行顺序
- Gradient descent, learning rate
- metaRTC4.0集成ffmpeg编译
- ZABBIX proxy, sender (without agent monitoring), performance optimization
- Clause 31: avoid default capture mode
- QT using layout manager is invalid or abnormal
猜你喜欢
float类型取值范围
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Case - count the number of occurrences of each string in the string
Small project - household income and expenditure software (1)
About anonymous inner classes
Ruoyi cloud startup tutorial (hand-held graphics)
C language learning log 12.5
Chapter 15 mechanism: Address Translation
metaRTC4.0稳定版发布
metaRTC4.0集成ffmpeg编译
随机推荐
Section 5 - Operator details
【转载】C语言内存和字符操作函数大全
Clause 30: be familiar with the failure of perfect forwarding
Luogu p3654 fisrt step
System file interface open
KVM virtualization management tool
Case - random numbers without repetition (HashSet and TreeSet)
BM1Z002FJ-EVK-001开机测评
Luogu p1012 guess
安装harbor(在线|离线)
RT thread console device initialization
Violence enumeration~
Logical point
Brick story
Understanding of speech signal framing
Clause 27: alternatives to overloading with familiar universal reference types
Course outline of market drawing 1- basic knowledge
C language learning log 1.19
C language learning log 11.7
File descriptorfile description