Common Mysql Statements



Create a table named yingXiong and fill in the corresponding data. The above operations are performed in the navicat view tool:

 

  

 

  

1. Query the entire table

slelect * from yingXiong;

2. Query the value of one or more fields

select y_name from yingXiong;

select y_name ,y_sex from yingXiong;---Separate the fields to be queried with commas (,)

3. Conditional query and logical operator

select * from yingXiong where y_age<30;

select * from yingXiong where y_shuchu='800';

Note: Logical operator: < = > <=>=!= is the same as <>, the value of the condition in the conditional statement needs to be added with single quotation marks ('')

4. Multi-condition query

select * from yingxiong where y_sex='male' and y_age>'30';

select * from yingxiong where y_sex='male' and y_age>'30' and y_type='adc';

select * from yingxiong where y_sex='female' or y_type='adc '; --- Only one of the two conditions is satisfied

5. Sort query

select * from yingxiong order by y_age;

The order by keyword defaults to positive sorting;

if you want to reverse the sorting, you need to add the desc keyword: select * from yingxiong order by y_age desc;

positive order sorting can also use the (asc) keyword: select * from yingxiong order by y_age asc;

6 、Interval query

select * from yingxiong where y_age >='18' and y_age<='30';--- query hero information between 18 and 30 years

select * from yingxiong where y_age between '18' and '30' ;

The above two statements have the same query effect

between ... and ...

7. Set query

select * from yingxiong where y_age in(18,25);

The in keyword indicates a set, and the set range is filled in the brackets after in

8 , Fuzzy query

select * from ying xiong where y_name like '%Li%';

use Like keyword, which means fuzzy, followed by '%fuzzy content%', the front is which field to perform fuzzy query according to

Note : '%Li %', which means that as long as the field contains the word Li, it will be queried out

'Li%' , which means that as long as the first character of the field contains Li, it will be queried

'% Li', which means that as long as the last character of the word contains Li, it will be queried.

9. Paging query

select * from yingxiong limit 1,4;

the limit keyword means paging, followed by two numbers 0,4 after the keyword, 0 means starting from the number with the subscript number, 4 means how many items are displayed in the current page

The data is separated by commas (English commas)

If the limit keyword is followed by a number, it indicates how many numbers are displayed on the current page. If it is 7, it will display 7 statements

. 10. Simple combined query

select * from yingxiong where y_age<'30 ' and y_name like '%qiao%' order by y_age limit 0,2;

pay attention to the order of keyword combinations, the limit keyword must be placed at the end of the statement

11. Name the alias

select y_name as 'name' from yingxiong;

as in The keyword indicates a named alias. Before the keyword is the field or table to be named, followed by the alias

select y_name 'name' from yingxiong;

the as keyword can also be omitted, but there must be a space between the original name and the alias.

12. Commonly used aggregation functions

a, find the maximum value: select max(y_age) from yingxiong;

max keyword means the maximum

b, find the minimum value: select min(y_age) from yingxiong;

min means the minimum

c, find the average value: select avg(y_age) from yingxiong;

avg keyword means average

d, summation: select sum(y_age) from yingxiong;

sum keyword means summation

e, count how many pieces of data there are: select count(y_age ) from yingxiong; The

count keyword means to find the total number.

You can also write a statement like this: select count(*) from yingxiong;

13. Sub-condition query

select * from yingxiong where y_age=(select max(y_age) from yingxiong)

; The result is used as a condition.

14. Group query

select y_type, sum(y_age) from yingxiong group by y_type; The

group by keyword indicates grouping, grouping according to the value of a certain field, and the same value forms a group.

Note: The grouping function is generally used together with the aggregation function.

15. Grouping plus conditional query

select y_type, sum(y_age) as ageHe from yingxiong group by y_type having ageHe>'60';

having keyword indicates the condition, which is the key to the professional condition of the grouping function Character

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326398994&siteId=291194637