MySQL select query

The syntax and MySQL database CRUD: https://blog.csdn.net/sabstarb/article/details/105059672

table of Contents

First, deduplication

Two, where conditions

Third, the United-table query

Fourth, since the connection

Fifth, sorting and paging

Six Subqueries

Seven, commonly used functions

八、group by


First, deduplication

Check out the removal of select statement repeated portion (keyword distinct);

Statement: select [distinct] field name form the table name

例:select distinct name from people;

Here Insert Picture Description

Two, where conditions

Role: Returns the specified results based on query

1, logical operators

Operators grammar description
and 和 && result >1 and result <4 === > result >1 && result <4 And logic, two conditions are met, it is true
or 和 || result >1 or result <4 ===> result >1 || result <4 Or logical, satisfying a condition is true
and not! not result = 2 ===> ! (result = 2) Negated, result is not equal to 2, is true

And operation: All conditions must be met

Here Insert Picture Description
Or operation: only need to meet one of the conditions to
Here Insert Picture Description

Invert: When the condition is satisfied, the return portion does not satisfy the conditions (remember to use parentheses ()!)

Here Insert Picture Description
2, comparison operators

Operators grammar description
is null name is null Query name is null data
is not null name is not null Query name is not null data
between…and… id between 2 and 4 Between query id data 2-4
like (fuzzy query) name like b (there are two kinds of modifiers and _%) Matching, whether there is a data name b, a modifier may be a partial match
in name in (a1, a2, a3, a4 ...) (exact match) Matching, a1 to a4 data exists in name returns true, exact match

is null and not null IS:
Here Insert Picture Description
the BETWEEN ... and ...: query data between the ages of 20-40 years of age
Here Insert Picture Description
like:% (0 to represent any characters)

% With long as the data does not see the length thereof, such as '%% 2', 2 will be detected with a long query.

Here Insert Picture Description
like: _ (represents a character)

_ With data, a character will ignore the query, beyond a character will not match, such as (Wang _), as long as there are two characters to the back, it will not match.
Here Insert Picture Description
in: exact match plurality of data, equivalent to multiple queries.
Here Insert Picture Description

Third, the United-table query

JOIN

Here Insert Picture Description

contingency table join operation

operating description
inner join If there is at least one match in the table, it returns OK
left join To the left table as a reference, even if there is no match to the right table, the table has left, will output
right join To the right table as a reference, even if there is no match to the left table, right table there are also output

the Join Inner: (equal to two table data acquired under the conditions rows)
Here Insert Picture Description
left the Join: a reference table has been left, the value of acquisition, the data table even if the two are not equal, the table data subject to the left.
Here Insert Picture Description
right join: have the right table as a reference, get the value, even if the two tables of data are not equal to the data subject the right table.
Here Insert Picture Description
join on: join query

where: Equivalent inquiry

and where on the use (efficient culling can not meet the conditions of data) in combination:

  • 1, where is created in a temporary table, a linked list processing operation

  • 2, but the temporary table has been completed, where then filter the data, it has led to the emergence of ideal.

Here Insert Picture Description

But when on and and, or be used in combination, the effect obtained is not satisfactory to (or is not supported in the join):

  • 1, and is filtered join in a temporary table, the data to be limiting

  • 2, when the join, and the restrictions on data connection process, but because the data has been processed, not all matching results in a null data.
    Here Insert Picture DescriptionHere Insert Picture Description

Fourth, since the connection

Core: A table is split into two tables.
Here Insert Picture Description

Split: According to a sub-account id, subjects get the parent, the other sub-accounts, for parent subclass id = id.

Here Insert Picture Description
Here Insert Picture Description

Fifth, sorting and paging

1, the sorting Syntax: order by field (the ASC (ascending) or DESC (descending))

Example: to check out the data in descending order.

select p.pid,s.subjectname as ‘父科目',p.subjectname  as ‘子科目’
from subject as s inner join subject as p 
on s.subjectid = p.pid order by p.pid desc;

Here Insert Picture Description
2, SQL statement in the last page limit

The subscript mysql from zero by default, oracle from the beginning.

Syntax: limit the query index (corresponding page of the current page), display data (the number of page data display);

  • Web page query the general method of calculation (web page display index starts at 0): Query subscript (this page) * display data, display data;

  • A general web page query calculated (start page display index tab from 1) :( query index (this page) -1) * display data, the display data;

Example: the data after completion of descending paging, data 5Records

select p.pid,s.subjectname as ‘父科目',p.subjectname  as ‘子科目’
from subject as s inner join subject as p 
on s.subjectid = p.pid order by p.pid desc limit 0,5;

Here Insert Picture Description

Six Subqueries

Essence: In nesting in where a select statement.

1, sub-queries by a small number of tables, query data more tables (tables in the query data must be in memory).

2, the table present field of other tables (eg: id) can be achieved, but only acquires the corresponding fields in this table (eg: id), not other fields of other tables.

例:
select peopleid,name,roleId(其他表的ID)from people(只能查询people存在的数据)
where roleId in (select roleId from role where roleId >0);

Here Insert Picture Description
Sub by roleId and studentid people table queries
people table data:
Here Insert Picture Description
query job as an ordinary employee, gender woman data:
Here Insert Picture Description
subquery although simple, but not linked table query that can query data from other tables (but using join my personal United almost feel and table lookup), is different.

Seven, commonly used functions

1, time

function description
select current_date() Get the current time (date)
select curdate() Get the current time (date)
select now() (Month, day, minutes and seconds) to get the current time
select localdate() (Local time date when minutes and seconds) to get local time
select sysdate() (Minutes and seconds the time when the system date) Get the system time
select year(now) Get the current year
select month(now) Get the current month
select day(now) Get the current date
select hour(now) Get the current hour
select minute(now) Get the current minute
select second(now) Get the current number of seconds

2, the system

function description
select system_user() Get the current user
select user() Get the current user
select version() Get the current version number

Here Insert Picture Description
3, common aggregate functions

function description
count count
sum Summing
avg average value
max Maximum
me Minimum

count efficiency: count (column name)> count (1)> count (*)

count (column name): Query only the specified column, ignoring null.

count (1): Only the first column of the query, do not ignore null.

count (*): Each row of the query, do not ignore null.
Here Insert Picture Description

select sum(列名) [as 总分] from 表名
select avg(列名) [as 平均分] from 表名
select max(列名) [as 最高分] from 表名
select min(列名) [as 最低分] from 表名

4, MD5 encryption

md5 encryption is an encryption means to encrypt data in data insertion, for verification encrypted data (encrypted data will not change every time the same data encryption, data is encrypted the same value), Java is generally inserted into the encrypted.

insert into user(userid,username,password) values('2','wangwu',mad5('13579'));

Here Insert Picture Description

八、Group by

1, group by column names: column name by grouping, in the following where.

  • will be the specified group by the same column merge data, but in a plurality of cell id, clearly the use of, so a direct query id error.

Here Insert Picture Description

Here Insert Picture Description
group by a polymerization operation and function:
Here Insert Picture Description
2, HAVING: secondary filter conditions, and with the group by filtering the data group by the return, make up where the function can not be used in conjunction with the polymerization.

select the full statement:

selete [all(默认) | ditinct(去重)]*(全部),指定列名(可以有多个)[as 别名]from (表名 [as 别名][(inner | left | right ) join (表名 [as 别名][on ...条件]] ===>联合查询
 
 [where ...条件]
 
 [group by 列名] ===> 根据列名进行分组,数据相同为一组进行计数)
 
 [having ...次要条件] ===>group by一起,过滤group by返回的数据,弥补where不能和聚合函数联合使用的不足
 
 [order by ( asc (升序) | desc (降序) )]
 
 [limit 查询页码(当前页),展示数据个数] 

back to the top

Published 61 original articles · won praise 0 · Views 2163

Guess you like

Origin blog.csdn.net/sabstarb/article/details/105080290