MySQL database _ data query _ paging, link query

MySQL database

Pagination

Get some rows

  • When the amount of data is too large, it is very troublesome to view the data on one page

grammar

select * from 表名 limit start,count

Description

  • From start, get count data
--限制查询出来的个数
select * from students where gender=1 limit 2;

--查询前5个数据
select * from students limit 0, 5;

--查询6-10(包含)的数据
select * from students limit 5, 5;



--每页显示2个,第1个页面
select * from students limit 0, 2;

--每页显示2个,第2个页面
select * from students limit 2, 2;

--每页显示2个,第3个页面
select * from students limit 4, 2;

--每页显示2个,第4个页面
select * from students limit 6, 2;    -- ---> limit (第n页-1)*每个的个数,每一页的个数;

--每页显示2个,显示第6也的信息,按照年龄从小到大排序
--错误的查询方法1:select * from students limit 2*(6-1), 2;
--错误的查询方法2:select * from students limit 10, 2 order by age asc;

select * from students order by age asc limit 10, 2;

select * from students where gender=2 order by height desc limit 0, 2;

Link query

  • When the column of the query result comes from multiple tables, you need to join the multiple tables into a large data set, and then select the appropriate column to return
  • It is generally used for multi-table query, but it is almost useless when single-table query.

mysql supports three types of connection queries, namely:

  • Inner join query: the result of the query is the data matched by the two tables

innerjoin

  • External links are divided into "left link and right link"

    • Left join query: the result of the query is the data matched by the two tables, the data unique to the left table, and the data that does not exist in the right table is filled with null

    leftjoin

    • Right join query: the result of the query is the data matched by the two tables, the data unique to the right table, and the data that does not exist in the left table is filled with null

    rightjoin

grammar

select * from 表1 inner或left或right join 表2 on 表1.列 = 表2.列

Example of internal link query:

  • take交集
--查询 有能够对应班级的学生以及班级信息
select * from students inner join classes on students.cls_id=classes.id;

--按照要求显示姓名、班级
select students.*,classes.name from students inner join classes on students.cls_id=classes.id;

select students.name,classes.name from students inner join classes on students.cls_id=classes.id;

--给数据表起名字
select s.name,c.name from students as s. inner join classes as c on s.cls_id = c.id;

--查询 有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称
select s.*,c.name from student as s inner join classes as c on s.cls_id=c.id;

--在以上的查询中,将班级姓名显示在第1列
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;

--查询 有能够对应班级的学生以及班级信息,按照班级进行排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;

--当是同一个班的时候,按照学生的id进行从小到大排序
select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;

Example of left link query:

  • In the table on the left, take the table as the benchmark to get the data, if you can’t get it, the default isNULL
--查询每位学生对应的班级信息
select * from students as s left join classes as c on s.cls_id=c.id;

--查询没有对应班级信息的学生
select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;

select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;
  • Use havingand wherecan be achieved, but the difference between the two where is it?
    • If the result is judged from the original table, then use where
    • If the new result from the search is regarded as a result set, the result is judged in the result set, then use having

Example of right link query:

  • In the table on the right, take the table as the benchmark to get the data. If you can't get it, the default isNULL
  • Under normal circumstances, it is not commonly used, because the names of the two tables are swapped, and left join can also be used to complete

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90423457