本系列文章用于记录MySQL学习过程中漏掉的知识点,进行查漏补缺。
知识点来源:《MySQL必知必会》、b站-李玉婷-MySQL基础
一、limit
1、带一个值的limit总是从第一行开始,给出的数为返回的行数
select prod_name from products limit 5;
#返回前五行数据
2、带两个值的limit可以指定从行号为第一个值的位置开始(偏移量从0开始)
select prod_name from products limit 2,5;
#返回从行2(第3行)开始的5行
3、分页查询
公式:
要显示的页数page ,每页显示的条数size;
select 查询列表 from 表
limit (page-1)*size , size;
解析:
找规律
size=10(每页显示10条)
page:
1 — 0
2 — 10
3 — 20
二、order by
可进行多列排序,可指定排序方向
desc 降序
esc 升序(默认,可省略)
#多列排序
select prod_id,prod_price,prod_name from products order by prod_price,prod_name;
#指定排序方向
select prod_id,prod_price,prod_name from products order by prod_price desc;
#多列指定方向排序
select prod_id,prod_price,prod_name from products order by prod_price desc,prod_name;
注:若想在多个列上进行降序排序,必须对每列指定desc关键字
三、where
此外还有and ; or ; in ; not in
1、空值检查(is null)
select cust_id from customers where cust_email is null;
2、and or 计算次序
where 可包含任意数目的and 和or 操作符