Query select

Query select

1. Grammar:

select * from 表名; //查询全部
select 列名,列名,...   from  表名;   

2. Make an alias

Syntax: column name AS alias;

3. Conditional query

select  列名   from  表名   where 条件 

Logical operation :

AND	  与;
OR	  或;
NOT	  非

4. Scope query :

BETWEEN ...  AND  ...  在两个值之间(包含边界)

IN(set,set,...) 	等于 值列表中的一个

5. Fuzzy query: LIKE

​% represents zero or more characters (any number of characters).

​ _ represents a character

6. Paging query : limit

limit  [offset] size;
	offset代表的是起始的条目索引,默认从0开始
	size代表的是显示的条目数

​ Formula: limit (current page number -1) * number per page, number per page;

7. Sort ORDER BY

​ ASC ascending order DESC descending order

8. Aggregate functions

sum summation; avg average value; max maximum value; min minimum value; count count

9. Subqueries

select 列名  from  表 子查询

​ The result of the subquery is used as a table, and an alias must be required

10.SQL Association

​ One-to-one; one-to-many; many-to-many;

Primary key PRIMARY KEY, the value of this field cannot be repeated and is not unique+not null

Foreign key FOREIGN KEY, the value of this field refers to the field of another table

外键语法:FK_引用表\_连接表

11. Query with tables

classification:

​ Internal connection;

​ Outer connection: left outer connection; right outer connection

11.1. Internal connection syntax:

select 查询列表
from 表1 ,表2 
where 表1.key=表2.key

​ Internal connection main table: the table after the comma

11.2. Outer join syntax:

	select 查询列表
	from 表1 left join  表2 
	on 表1.key=表2.key

Overview of external connections:

外连接,主表的数据都会显示,不论从表有没有对应的数据。

左外连接,左边的表数据都会显示。

右外连接,右边的表数据都会显示。

left join 左边的就是主表,right join 右边的就是主表

SQL optimized syntax:

​ 1. Do not judge the database for null (judgment of null value);

​ 2. Comparison operations will reduce the efficiency of the database, and may cause index failure;

​ 3. The use of left blur is generally not allowed, and the use of left blur will cause the index to become invalid;

​ 4. When the inner connection query, the data dictionary table is the main table (the table after the comma);

5. Try not to use or.

Guess you like

Origin blog.csdn.net/muhaokai/article/details/115321962