Structured Query Language 结构化查询语言,一门操作关系型数据库的语言。
其定义操作所有关系型数据库的统一标准。
目前常见的关系型数据库系统有IBM的DB2、甲骨文的Oracle和MySQL 、微软的MSSQL和Access、Sybase的Sybase等。
此篇文章基于MySQL
对SQL
语法及命令展开描述。
可以单行或多行书写,;
是语句结束的标志。
MySQL数据库的SQL语句不区分大小写,关键字建议使用大写。
注释 使用--
(两个短横线加一个空格)或#
作为单行注释,使用/*语句*/
作为多行注释。
mysql> SHOW DATABASES;-- 展示MySQL中的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.01 sec)
DDL(Data Definition Language)数据定义语言,用来定义数据库对象:数据库,表,列。
DML(Data Manipulation Language)数据操作语言,用来对数据库中表的数据进行增删改。
DQL(Data Query Language)数据查询语言,用来查询数据库中表的数据。
DCL(Data Control Language)数据控制语言,用来定义数据库的访问权限和安全级别及创建用户。(注意:本文不涉及DCL)
操作数据库,表等。
查询 SHOW DATABASES;
mysql> SHOW DATABASES;-- 展示MySQL中的数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.01 sec)
创建
-创建数据库
CREATE DATABASE 数据库名称;
-如果不存在则创建数据库
CREATE DATABASE IF NOT EXISTS 数据库名称;
mysql> CREATE DATABASE IF NOT EXISTS db4;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| db4 |
| mysql |
| performance_schema |
| sys |
+--------------------+
8 rows in set (0.00 sec)
删除
-删除数据库
DROP DATABASE 数据库名称;
-如果存在则删除数据库
DROP DATABASE IF EXISTS 数据库名称;
mysql> DROP DATABASE IF EXISTS db4;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db1 |
| db2 |
| db3 |
| mysql |
| performance_schema |
| sys |
+--------------------+
7 rows in set (0.00 sec)
使用
-查看当前使用的数据库
SELECT DATABASE();
-使用数据库
USE 数据库名称;
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
mysql> USE db2;
Database changed
mysql> SELECT DATABASE();
+------------+
| DATABASE() |
+------------+
| db2 |
+------------+
1 row in set (0.01 sec)
CREATE TABLE 表名称(
字段名1 数据类型1,
字段名2 数据类型2,
...
字段名n 数据类型n
);
SQL提供了很多种数据类型,常见有int
、char
、varchar
、date
和double
。
mysql> CREATE TABLE student(
-> name varchar(10),
-> gender char(1),
-> score double(5,2)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
为了保证插入表中数据的合法性,数据库管理系统会对插入表中的数据进行检验。可以在创建表的同时为表中的属性添加约束
。
mysql> CREATE TABLE student(
-> id int primary key auto_increment,
-> name varchar(20) not null,
-> status int default 0,
-> person_id varchar(34) not null unique
-> );
Query OK, 0 rows affected (0.03 sec)
mysql>
mysql> desc student;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| status | int(11) | YES | | 0 | |
| person_id | varchar(34) | NO | UNI | NULL | |
+-----------+-------------+------+-----+---------+----------------+
可以发现,与创建表不同的是:在字段名和数据类型后紧跟了一些关键字–约束条件
。
常见的约束条件有:
not null -- 非空约束 即:插入到当前字段内容不能为空
primary key -- 主键 即:表中行数据唯一指定关键字
auto_increment -- 自增 即:无需设定当前字段内容,会随着数据的增加自增
unique -- 独一无二 即:当前字段内容不能重复
CHECK () -- 检验 即:根据()内条件判断语句判断插入数据是否非法
DEFAULT 值 -- 默认值 即:未设定值时会根据默认值进行赋值
关系型数据库是通过表来存储数据的,但有时单独的表并不能很好的完成对常见数据的建模,表与表之间是存在关系的。
通过对表与表添加外键
可以在表与表之间建立联系。
创建表时添加外键语法:CONSTRAINT 外键名 FOREIGN KEY(待关联字段名) REFERENCES 待关联表名(待关联表的主键字段名);
使用命令语句添加外键语法:ALTER TABLE 表名 ADD CONSTRAINT 外键名 FOREIGN KEY(待关联字段名) REFERENCES 待关联表名(待关联表的主键字段名);
创建一张课程表以及一张学生表,学生表中的s_id
一一对应着课程表中主键值为s_id的课。
mysql> CREATE TABLE course(
-> id int primary key auto_increment,
-> course_name varchar(34) not null unique,
-> teacher varchar(30) not null
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> CREATE TABLE student(
-> id int primary key auto_increment,
-> name varchar(30) not null,
-> status int default 0,
-> s_id int,
-> CONSTRAINT fk_stu_course FOREIGN KEY(s_id) REFERENCES course(id)
-> );
Query OK, 0 rows affected (0.02 sec)
两张表一旦建立联系,插入以及删除表中内容时便不能“随心所欲”。例如:向student表中添加数据时course表必须已经存在否则便会报错: Cannot add or update a child row: a foreign key constraint fails…。想要删除课程表中的某一节课时,也必须保证已经没有学生属于这堂课,否则也是非法操作。
也许此时你会觉得外键很限制对表的操作,非常不好用。但正是这样,维护了表与表之间的关系,使得多个表之间建立联系。
合法添加数据:
mysql> INSERT INTO course(course_name,teacher) VALUES
-> ("C Language","wiwi"),
-> ("English","siri"),
-> ("Physics","jing");
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> INSERT INTO student(name,status,s_id) VALUES
-> ("zhangsan",1,1),
-> ("lisi",1,3),
-> ("laoliu",0,2);
Query OK, 3 rows affected (0.00 sec)
想要删除外键时,删除外键语法:ALTER TABLE 表名 DROP FOREIGN KEY 外键名;
(1)查询当前数据库下所有表的名称
SHOW TABLES;
mysql> SHOW TABLES;
+---------------+
| Tables_in_db2 |
+---------------+
| cars |
| dept |
| emp |
| student |
+---------------+
4 rows in set (0.00 sec)
(2)查询表结构
DESC 表名称;
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
修改之前先得有一个表,创建一个表。
mysql> CREATE TABLE student(
-> name varchar(10),
-> gender char(1),
-> score double(5,2)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> DESC student;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
(1)修改表名
ALTER TABLE 表名 RENAME TO 新表名;
mysql> ALTER TABLE student RENAME TO students;
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW TABLES;
+---------------+
| Tables_in_db2 |
+---------------+
| cars |
| dept |
| emp |
| students |
+---------------+
4 rows in set (0.00 sec)
(2)添加一列
ALTER TABLE 表名 ADD 列名 数据类型;
mysql> ALTER TABLE students ADD address varchar(34);
Query OK, 0 rows affected (0.03 sec)
mysql> DESC students;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
| address | varchar(34) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
(3)修改数据类型
ALTER TABLE 表名 MODIFY 列名 数据类型;
mysql> ALTER TABLE students MODIFY address char(34);
Query OK, 0 rows affected (0.03 sec)
mysql> DESC students;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
| address | char(34) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
(4)修改列名和数据类型
ALTER TABLE 表名 CHANGE 列名 新列名 新数据类型;
mysql> ALTER TABLE students CHANGE address addr varchar(64);
Query OK, 0 rows affected (0.03 sec)
mysql> DESC students;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
| addr | varchar(64) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
(5)删除列
ALTER TABLE 表名 DROP 列名;
mysql> ALTER TABLE students DROP addr;
Query OK, 0 rows affected (0.03 sec)
mysql> DESC students;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| name | varchar(10) | YES | | NULL | |
| gender | char(1) | YES | | NULL | |
| score | double(5,2) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
DROP TABLE 表名;
DROP TABLE IF EXISTS 表名;
mysql> DROP TABLE IF EXISTS students;
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW TABLES;
+---------------+
| Tables_in_db2 |
+---------------+
| cars |
| dept |
| emp |
+---------------+
3 rows in set (0.00 sec)
对表中的数据进行增删改。
首先新建一个表。
mysql> drop table if exists cars;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create table cars(
-> brand varchar(20),
-> price double(7,2)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| cars |
+---------------+
1 row in set (0.00 sec)
INSERT INTO 表名(列名1,列名2...列名n) VALUES(值1,值2...值n);
SELECT * FROM 列名;
下文会进行说明。
mysql> INSERT INTO cars(brand,price) VALUES("FORT",1000);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM cars;
+-------+---------+
| brand | price |
+-------+---------+
| FORT | 1000.00 |
+-------+---------+
1 row in set (0.00 sec)
不添加列名默认向所有列添加元素
INSERT INTO 表名 VALUES(值1,值2...值n);
mysql> INSERT INTO cars VALUES("BMW",2000);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM cars;
+-------+---------+
| brand | price |
+-------+---------+
| FORT | 1000.00 |
| BMW | 2000.00 |
+-------+---------+
2 rows in set (0.00 sec)
VALUES使用逗号隔开,批量添加数据
INSERT INTO 表名(列名1,列名2...列名n) VALUES(值1,值2...值n),(值1,值2...值n),(值1,值2...值n)...;
mysql> INSERT INTO cars(brand,price) VALUES
("FORT",1000),
("hello",500),
("SQL",1000);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM cars;
+-------+---------+
| brand | price |
+-------+---------+
| FORT | 1000.00 |
| BMW | 2000.00 |
| FORT | 1000.00 |
| hello | 500.00 |
| SQL | 1000.00 |
+-------+---------+
5 rows in set (0.00 sec)
UPDATE 表明 SET 列名1=值1,列名2=值2...列名n=值n WHERE 条件;
注意:使用WHERE
关键字限制修改范围否则会全部修改!
mysql> UPDATE cars SET brand="Jeep" WHERE brand="hello";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> SELECT * FROM cars;
+-------+---------+
| brand | price |
+-------+---------+
| FORT | 1000.00 |
| BMW | 2000.00 |
| FORT | 1000.00 |
| Jeep | 500.00 |
| SQL | 1000.00 |
+-------+---------+
5 rows in set (0.00 sec)
DELETE FROM 表名 WHERE 条件;
注意:使用WHERE
关键字限制删除范围否则会全部删除!
mysql> DELETE FROM cars WHERE brand="SQL";
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM cars;
+-------+---------+
| brand | price |
+-------+---------+
| FORT | 1000.00 |
| BMW | 2000.00 |
| FORT | 1000.00 |
| Jeep | 500.00 |
+-------+---------+
4 rows in set (0.00 sec)
对表中的数据进行查询。
(1) SELECT 列名1,列名2 FROM 表名;
mysql> SELECT brand FROM cars;
+-------+
| brand |
+-------+
| FORT |
| BMW |
| FORT |
| Jeep |
+-------+
4 rows in set (0.00 sec)
(2)当想查询每一列时可以使用*
占位。SELECT * FROM 表名;
mysql> SELECT * FROM cars;
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| FORT | 1000.00 | yellow | 2001-12-31 |
| BMW | 500.00 | green | 2014-08-10 |
| Jeep | 3000.00 | white | 1998-07-02 |
| Smart | 600.00 | blue | 2000-08-20 |
| BMW | 500.00 | green | 2014-08-10 |
+-------+---------+--------+------------+
5 rows in set (0.00 sec)
(3)使用DISTINCT
关键字进行去重查询。
SELECT DISTINCT 列名 FROM cars;
mysql> SELECT DISTINCT brand FROM cars;
+-------+
| brand |
+-------+
| FORT |
| BMW |
| Jeep |
| Smart |
+-------+
4 rows in set (0.00 sec)
(4)使用AS
关键字取“别名”。SELECT 列名1 AS 别名1,列名2 AS 别名2...列名n AS 别名n FROM cars;
注意AS
可以省略(如:下面代码中dates的别名),但至少要用空格隔开。
mysql> SELECT brand AS 品牌,color AS 颜色,dates 生产日期 FROM cars;
+--------+--------+--------------+
| 品牌 | 颜色 | 生产日期 |
+--------+--------+--------------+
| FORT | yellow | 2001-12-31 |
| BMW | green | 2014-08-10 |
| Jeep | white | 1998-07-02 |
| Smart | blue | 2000-08-20 |
| BMW | green | 2014-08-10 |
+--------+--------+--------------+
5 rows in set (0.00 sec)
(5)使用WHERE
关键字进行条件查询。
SQL
中提供了许多逻辑运算符表示合取的有&&
和AND
、表示析取的有||
和OR
。表示关系的有>=
、<=
和=
。注意没有==
。SQL中还提供了BETWEEN...AND...
语句限定区间。
mysql> SELECT * FROM cars WHERE price>=100 AND price<=1000;
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| FORT | 1000.00 | yellow | 2001-12-31 |
| BMW | 500.00 | green | 2014-08-10 |
| Smart | 600.00 | blue | 2000-08-20 |
| BMW | 500.00 | green | 2014-08-10 |
+-------+---------+--------+------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM cars WHERE price BETWEEN 100 AND 1000;
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| FORT | 1000.00 | yellow | 2001-12-31 |
| BMW | 500.00 | green | 2014-08-10 |
| Smart | 600.00 | blue | 2000-08-20 |
| BMW | 500.00 | green | 2014-08-10 |
+-------+---------+--------+------------+
4 rows in set (0.00 sec)
mysql> SELECT * FROM cars WHERE brand = "FORT"||brand = "Smart";
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| FORT | 1000.00 | yellow | 2001-12-31 |
| Smart | 600.00 | blue | 2000-08-20 |
+-------+---------+--------+------------+
2 rows in set (0.00 sec)
模糊查询,类似于字符串匹配。
通配符:_
可以代替任意单个字符,%
可以代替任意多个字符。
(1)查询品牌中第二字符为‘O’的车
SELECT * FROM 表名 WHERE 列名 LIKE 条件;
mysql> SELECT * FROM cars WHERE brand LIKE "_O%";
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| FORT | 1000.00 | yellow | 2001-12-31 |
+-------+---------+--------+------------+
1 row in set (0.00 sec)
(2)查询品牌中含有字符‘a’的车
SELECT * FROM 表名 WHERE 列名 LIKE 条件;
mysql> SELECT * FROM cars WHERE brand LIKE "%a%";
+-------+--------+-------+------------+
| brand | price | color | dates |
+-------+--------+-------+------------+
| Smart | 600.00 | blue | 2000-08-20 |
+-------+--------+-------+------------+
1 row in set (0.00 sec)
SELECT 字段列表 FROM 表名 ORDER BY 排序字段名1 排序方式1,ORDER BY 排序字段名2 排序方式2...ORDER BY 排序字段名n 排序方式n;
排序规则:ASC
默认升序,DESC
降序。默认排序规则可以省略。
(1)将所有汽车按价格升序排列
mysql> SELECT * FROM cars ORDER BY price ASC;
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| BMW | 500.00 | green | 2014-08-10 |
| BMW | 500.00 | green | 2014-08-10 |
| Smart | 600.00 | blue | 2000-08-20 |
| FORT | 1000.00 | yellow | 2001-12-31 |
| Jeep | 3000.00 | white | 1998-07-02 |
+-------+---------+--------+------------+
5 rows in set (0.00 sec)
(2)将所有汽车按价格升序排列,如果价格相同按日期降序排列。如果指定了多个排序方式只有靠前的排序方式相同时才会启用靠后的排序方式。
mysql> SELECT * FROM cars ORDER BY price ASC, dates DESC;
+-------+---------+--------+------------+
| brand | price | color | dates |
+-------+---------+--------+------------+
| BMW | 500.00 | green | 2014-08-10 |
| BMW | 500.00 | green | 2014-08-10 |
| Smart | 600.00 | blue | 2000-08-20 |
| FORT | 1000.00 | yellow | 2001-12-31 |
| Jeep | 3000.00 | white | 1998-07-02 |
+-------+---------+--------+------------+
5 rows in set (0.00 sec)
GROUP BY:分组查询。将表中的数据依照字段名进行分组,查询结果并统计。
(1)前置知识补充聚合函数
。
什么是聚合函数?聚合函数将一列的数据作为一个整体,进行纵向计算。
常见的聚合函数有sum(列名)
、avg(列名)
、max(列名)
、min(列名)
和count(列名)
。分别对应整列求和、求平均值、求最大值、求最小值和统计列数。
注意:聚合函数不会对null
值进行统计。
例如:执行SELECT COUNT(price) FROM cars;
只能统计到四列。
mysql> SELECT COUNT(price) FROM cars;
+--------------+
| COUNT(price) |
+--------------+
| 4 |
+--------------+
1 row in set (0.00 sec)
求个min试试。显然都不会对null
值进行统计,如果想要准确统计到底有多少列可以使用SELECT COUNT(*) FROM cars;
,因为*
会统计每一行含有非空值的行,这里需要思考一下。
mysql> SELECT MIN(price) FROM cars;
+------------+
| MIN(price) |
+------------+
| 500.00 |
+------------+
1 row in set (0.01 sec)
(2)分组查询。
注意分组字段名或聚合函数
这里跟其它查询内容是没有意义的,因为你不能在分组的前提下去查其它未分组的字段,但统计性的聚合函数是可以的。
实例1:将车按颜色分类,并输出每一类有几辆车。SELECT 分组字段名或聚合函数 FROM 表名 GROUP BY 分组字段名;
mysql> SELECT color,COUNT(color) FROM cars GROUP BY color;
+--------+--------------+
| color | COUNT(color) |
+--------+--------------+
| blue | 1 |
| green | 2 |
| white | 1 |
| yellow | 1 |
+--------+--------------+
4 rows in set (0.00 sec)
示例2:将车按颜色分类,并输出每一类有几辆车。另外,要求只统计时间在2000-1-1之后的车。
SELECT 分组字段名或聚合函数 FROM 表名 WHERE 条件 GROUP BY 分组字段名;
mysql> SELECT color,COUNT(color) FROM cars WHERE dates>'2000-1-1' GROUP BY color;
+--------+--------------+
| color | COUNT(color) |
+--------+--------------+
| blue | 1 |
| green | 2 |
| yellow | 1 |
+--------+--------------+
3 rows in set (0.02 sec)
示例3:将车按颜色分类,并输出每一类有几辆车。另外,要求只统计时间在2000-1-1之后的车,并且统计结果只输出所有颜色不为‘yellow’的数据。
SELECT 分组字段名或聚合函数 FROM 表名 WHERE 条件 GROUP BY 分组字段名 HAVING 输出过滤条件;
mysql> SELECT color,COUNT(color) FROM cars WHERE dates>'2000-1-1' GROUP BY color HAVING color != 'yellow';
+-------+--------------+
| color | COUNT(color) |
+-------+--------------+
| blue | 1 |
| green | 2 |
+-------+--------------+
2 rows in set (0.00 sec)
(3)WHERE + 条件
与HAVING +输出过滤条件
的区别。
1>执行时机不同:where在统计过程中执行,而having对统计结果进行过滤输出
2>可判断的条件不一样:where 不能对聚合函数进行判断, having 可以。
LIMIT分页查询:如果表中的数据过多不可能一次性展示出来,可以进行分页操作通过多页将表中数据输出。
对表进行扩充:
+----------+---------+--------+------------+
| brand | price | color | dates |
+----------+---------+--------+------------+
| FORT | 1000.00 | yellow | 2001-12-31 |
| BMW | 500.00 | green | 2014-08-10 |
| Jeep | 3000.00 | white | 1998-07-02 |
| Smart | NULL | blue | 2000-08-20 |
| BMW | 500.00 | green | 2014-08-10 |
| 奔驰 | 7000.00 | white | 2007-08-19 |
| 奥迪R8 | 7000.00 | green | 2004-08-19 |
| 长安 | 5000.00 | blue | 2007-12-19 |
+----------+---------+--------+------------+
分页查询语法:SELECT 字段列表 FROM 表名 LIMIT 起始索引, 单页查询条目数;
。规则:对不同页进行查询时遵循单页查询条目数
不变,改变起始索引。
示例1,要求每页3行元素,查询第二页。
mysql> SELECT * FROM cars LIMIT 3, 3;
+--------+---------+-------+------------+
| brand | price | color | dates |
+--------+---------+-------+------------+
| Smart | NULL | blue | 2000-08-20 |
| BMW | 500.00 | green | 2014-08-10 |
| 奔驰 | 7000.00 | white | 2007-08-19 |
+--------+---------+-------+------------+
3 rows in set (0.00 sec)
示例2,要求每页3行元素,查询第三页。
mysql> SELECT * FROM cars LIMIT 6, 3;
+----------+---------+-------+------------+
| brand | price | color | dates |
+----------+---------+-------+------------+
| 奥迪R8 | 7000.00 | green | 2004-08-19 |
| 长安 | 5000.00 | blue | 2007-12-19 |
+----------+---------+-------+------------+
2 rows in set (0.00 sec)
容易发现一个规律,那就是分页查询的查询公式:起始索引 = (当前页码数 - 1) * 每页显示条目数