MySQL basic query

1. Conditions inquiry

1.1 Introduction query conditions

Condition where the query statement is given in the query, the following may be used as keywords and operators where clause

  • =,! =, <> (Not equal to), <, <=,>,> =;
  • between ... and; equivalent to> = and <=
  • IN (set): in a query containing the data record
  • is null: empty is not null: not empty
  • and: connecting a plurality of conditions and for
  • or: or
  • not: negation

1. Query sex for women, and younger than 50 records

select * from stu
where gender='female' and age<50;

2. Query student number is S_1001, or the name of the record for the liSi

select * from stu 
where sid='S_1001' or sname='liSi';

3. Query student number is S_1001, S_1002, S_1003 record

select * from stu 
where sid in('S_1001','S_1002','S_1003');

4. Query student number is not S_1001, S_1002, S_1003 record

select * from stu
where sid not in('S_1001','S_1002','S_1003');

5. Query age null record

select * from stu
where age is null;

6. Query student records between the ages of 20-40

select * from stu
where age>=20 and age<=40;
select * from stu
where age between 20 and 40;

7. Query gender non-male student records

select * from stu
where gender!='male';
select * from stu
where gender<>'male';
select * from stu
where not gender='male';

8. Name the query is not null of student records

select * from stu
where not sname is null;
select * from stu
where sname is not null;

2. Fuzzy query

2.1 Overview

Keywords: like
syntax: like 'xxx';
like general and wildcards used with

2.2 Wildcard

%: Which matches one or more characters
_: which matches a character
[AZ]: represents any character within a matching range

2.3 Examples

1. Query student records by a five-letter name of

select * from stu
where sname like '_____';

2. Query name consists of five letters, and the first five letters "i" of student records

select * from stu
where sname like '____i';

3. Query the name to "z" at the beginning of student records

select * from stu
where sname like 'z%';

4. The second query letter "i" in the name of student records

select * from stu
where sname like '_i%';

5. Query name contains "a" letter of student records

select * from stu
where sname like '%a%';

3. The field control query

3.1 removal of duplicate records

Removing duplicates (more than two lines or rows of data are the same series), e.g. sal emp table there is the same record fields. When only the query fields sal emp table, then there will be duplicate records, then you want to remove duplicate records, use DISTINCT.

select distinct sal from emp;

Salary and commission of 3.2 employees to view and

select sal+ifnull(comm,0) from emp;

Add 3.3 to the column name alias

select sal+ifnull(comm,0) 
as 总工资,ename as 姓名 from emp;
select sal+ifnull(comm,0) 
总工资,ename 姓名 from emp;

3.4 splicing field

concat () function: connection field, the string is formed

select concat(ename,'的工资是:',sal+ifnull(comm,0))
from emp;

4. Sort

Keywords: order by; sort
syntax:
the Order by field asc: ascending order, default in ascending order: If you do not write, default ascending order.
order by field desc: descending order, desc must be written.
If there are multiple Sort: order by field 1 desc, asc field 2, ...
1. queries all student records, sorted in ascending order by age

select * from stu order by age asc;

2. Query all student records, sorted in descending order by age

select * from stu order by age desc;

3. Query all employees, sorted by salary in descending order, if the same salary, sorted in ascending order by number

select * from stu 
order by sal+ifnull(comm,0) desc,empno asc;

The aggregate function

Aggregation function is used to make longitudinal calculation function:
COUNT (): the number of rows NULL column is not specified statistics;
MAX (): maximum value of the specified column is calculated, if the specified column type is a string, then the string sorting using operation;
MIN (): minimum value of the specified column is calculated, if the specified column type is a string, then the string sorting operation;
the SUM (): calculated value and a specified column, if the specified type is not a numeric type column, the calculation result 0;
the AVG (): calculating an average value of the designated column, if the specified type is not a numeric type column, the calculation result is 0;

5.1COUNT

You may be used COUNT () 1. When a required longitudinal statistics.
The total number of records query the emp table:

select count(*) as cnt from emp;

Query the emp table has a number of commissions:

select count(comm) cnt from emp;

The number of queries monthly salary greater than 2500 emp table:

select count(*) from emp
where sal>2500;

People counting salary and commission greater than the sum of 2,500 yuan

select count(*) from emp 
where sal+ifnull(comm,0)>2500;

There are a number of inquiries the commission, and the number of leadership:

select count(comm),count(mgr) from emp;

5.2SUM and AVG

Discover all employees and salary:

select sum(sal) from emp;

Discover all employees and salary, as well as all employees and commissions

select sum(sal),sum(comm) from emp;

Discover all employees and salary + commission

select sum(sal+ifnull(comm,0)) from emp;

Statistics the average wage for all employees

select avg(sal) from emp;

6.MAX and MIN

Queries highest wages and the minimum wage

select max(sal),min(sal) from emp;

7. grouping queries

Grouped query: is the statistical data, aggregate functions in general can be used together! group by: grouping query
syntax: field group by group to the
execution sequence:
from table
where conditions
packet group by
HAVING conditions Polymeric filtering function
sort order by
1. Discover salary and department number of each sector of each sector, and

select deptno,sum(sal)
from emp
group by deptno;

2. The number of queries for each department and department number for each department

select deptno,count(*)
from emp
group by deptno;

3. Query department number for each department and each department the number of wage greater than 1500

select deptno,count(*)
from emp
where sal>1500
group by deptno;

4. Query greater than the sum sector wages and salaries and 9000

select deptno,sum(sal)
from emp
group by deptno
having sum(sal)>9000;

8.LIMIT

Used to define the query result LIMIT start line start, and the total number of rows size.
1. The Query 5 rows, starting from row zero

select * from emp limit 0,5;

2. Query 10 rows, starting from row 3 start

select * from emp limit 3,10;
Published 28 original articles · won praise 16 · views 597

Guess you like

Origin blog.csdn.net/qq_37881565/article/details/102665802