1: Query all fields
select from table_name;
2: Query specified fields
select name from goods;
select distinct type from goods;
If you want to query two fields in the table at the same time , Then the fields to be queried should be separated by commas
select id,name from goods;
3: Query specified records
select * from goods where id = 3;
4:
select * from goods where type = ' Sugars ';
5:
select * from goods where price =1230 ;
6:
select *from goods where price=2.5;
7:
select * from goods where add_time is NULL;
8:
select * from goods where num is not null;
9: Multi condition query ,MySQL Support multi condition query . If used between conditions and Keyword connection , Only records that meet all conditions will be returned . If conditions in multi condition query are used or
Keyword to connect , Indicates that only one of all conditions needs to be met , The record will be returned . among ,and Keywords can use symbols “&&” To replace ,or Keywords can use symbols “||” To replace .
select * from goods where type=' Sugars ' || type=' book ';
10: use in Keyword can query records whose field value is equal to any value in the specified set , The grammatical form is
select * from table_name where col_name in (value1,value2,....valuen);
or Keywords and in Keywords can achieve the same function , however in Keywords can make query statements more concise , also in Keyword execution is faster than or Keywords fast . in addition ,in
Keywords can also be associated with not Keywords used together , It is used to query records whose field value is not in the specified set .
select * from goods where id in (1,2,5,8,9);
11: Range query , Used is between keyword , Used to query records with field values within a certain range , The grammatical form is
select * from tablename where col_namea between value1 and value2;
select * from goods where price between 1 and 50;
12:
select * from goods where price >=1 and price <=50;
13:
select * from goods where price<=1 or price>=50;
14: Character matching query , use like Keyword is also called fuzzy query , It is usually used to query records whose field values contain some characters , The grammatical form is
select * from table_name where colnamea like valueb;
among ,vvalueb Represents the character to match ,like keyword General and wildcards “%” perhaps “_” Use together , If field col_namea Median contains
valueb, This record will be returned , Wildcards can be placed before characters , It can also be placed after characters , It can also be placed before and after characters . wildcard “%” Can match characters of any length , Can be 0 individual , It can also be one or more .
select * from goods where name like '% juice ';
15: No duplicate query results :MySQL Provided distinct keyword , Make the query results not repeated , Its grammatical form
select distinct col_list from table_name;
16: Single field sorting implement sql sentence , query goods In the table id,name and add_ Data of field , And according to add_time Sort by field value , for example
select id,name,add_time from goods order by add add_time;
17: Multi field sorting , query goods All records in the table , And according to price and num Sort by field value , for example
select * from goods order by price,num;
You can see from the query results , The system will first follow price Sort by field value , about price Records with the same field value , Then according to num Sort by field value .
18: according to num Ascending sum of fields add_time Descending order of fields
select * from goods order by num asc,add_time desc;
19: implement sql sentence , take goods Find all the records in the table , And according to price Field descending sort
select * from goods order by price desc;
When sorting by multiple fields , You can also use desc Sort keywords in descending order .
Technology