让我们首先创建一个表-
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, StudentName varchar(20) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable(StudentName) values('John'); mysql> insert into DemoTable(StudentName) values('Carol'); mysql> insert into DemoTable(StudentName) values('David'); mysql> insert into DemoTable(StudentName) values('Bob'); mysql> insert into DemoTable(StudentName) values('Chris'); mysql> insert into DemoTable(StudentName) values('Robert'); mysql> insert into DemoTable(StudentName) values('Adam'); mysql> insert into DemoTable(StudentName) values('Mike'); mysql> insert into DemoTable(StudentName) values('Sam');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable;
输出结果
+----+-------------+ | Id | StudentName | +----+-------------+ | 1 | John | | 2 | Carol | | 3 | David | | 4 | Bob | | 5 | Chris | | 6 | Robert | | 7 | Adam | | 8 | Mike | | 9 | Sam | +----+-------------+ 9 rows in set (0.00 sec)
这是选择特定行MySQL的查询-
mysql> select *from DemoTable ORDER BY Id DESC LIMIT 4 OFFSET 2;
输出结果
+----+-------------+ | Id | StudentName | +----+-------------+ | 7 | Adam | | 6 | Robert | | 5 | Chris | | 4 | Bob | +----+-------------+ 4 rows in set (0.00 sec)