当前位置:网站首页>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;
原网站

版权声明
本文为[strive_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280514548850.html