检索方法
select语句
无顺序检索:
检索单列:
输入▼
#从books表中检索名为author的列。
SELECT author FROM books;
输出▼
#查询单列的结果:
+----------------+
| author |
+----------------+
| Pruitt, et al. |
| Michael Morgan |
| Thomas Down |
| Thomas Schenk |
+----------------+
检索多列:
输入▼
#从books表中检索名为author,title,price的三列。
SELECT author,title,price
FROM books;
输出▼
#查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
+----------------+---------------------------------------------------+-------+
检索所有列:
使用通配符~
输入▼
#从books表中检索所有的列。
SELECT *
FROM books;
输出▼
#查询所有列的结果:
+---------------+----------------+---------------------------------------------------+-------+
| isbn | author | title | price |
+---------------+----------------+---------------------------------------------------+-------+
| 0-672-31509-2 | Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| 0-672-31697-8 | Michael Morgan | Java 2 for Professional Developers | 34.99 |
| 0-672-31745-1 | Thomas Down | Installing Debian GNU/Linux | 24.99 |
| 0-672-31769-9 | Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
+---------------+----------------+---------------------------------------------------+-------+
检索不同的值:
不希望每个值每次都出现时使用的方法~
则使用关键字 DISTINCT 来让输出结果具有唯一性~
注意:DISTINCT关键字是作用于所有的列,只有所有选定列都相同时才会只显示一个。
- 一般查看方法:
输入▼
#从persons表中检索ages列。
SELECT ages
FROM persons;
输出▼
#查询ages列的结果:
+------+
| ages |
+------+
| 18 |
| 18 |
| 8 |
| 19 |
| 45 |
| 8 |
+------+
- 只显示一次的查看方法:
输入▼
#从persons表中检索ages列。
SELECT DISTINCT ages
FROM persons;
输出▼
#查询无重复ages列的结果:
+------+
| ages |
+------+
| 18 |
| 8 |
| 19 |
| 45 |
+------+
顺序检索:
顺序检索单列:
- 无顺序查看方法:
输入▼
#从persons表中检索ages列。
SELECT ages
FROM persons;
输出▼
#无顺序查询ages列的结果:
+------+
| ages |
+------+
| 18 |
| 18 |
| 8 |
| 19 |
| 45 |
| 8 |
+------+
- 顺序查看方法:
输入▼
#顺序从persons表中检索ages列。
SELECT ages
FROM persons
ORDER BY ages;
输出▼
#顺序查询ages列的结果:
+------+
| ages |
+------+
| 8 |
| 8 |
| 18 |
| 18 |
| 19 |
| 45 |
+------+
顺序检索多列:
- 无顺序查看方法:
输入▼
#从persons表中检索ages列。
SELECT author,title,price
FROM books;
输出▼
#无顺序查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
+----------------+---------------------------------------------------+-------+
- 顺序查看方法:
输入▼
#顺序从books表中检索多列。
SELECT author,title,price
FROM books
ORDER BY title,price;
#将按照先title排序,title相同时再price的顺序输出/
输出▼
#顺序查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
+----------------+---------------------------------------------------+-------+
按列位置排序:
不使用别名,使用按相对列位置进行排序~
- 无顺序查看方法:
输入▼
#从persons表中检索ages列。
SELECT author,title,price
FROM books;
输出▼
#无顺序查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
+----------------+---------------------------------------------------+-------+
- 顺序查看方法:
输入▼
#顺序从books表中检索多列。
SELECT author,title,price
REOM books
ORDER BY 2,1;
#将按照先相对列为2进行排序,相对为2的列相同时再按相对列为1的顺序输出/
输出▼
#顺序查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
+----------------+---------------------------------------------------+-------+
倒序排序:
使用关键字 DESC 实现相反的顺序(从大到小)排序输出
注意:对多个列进行降序排序时,必须对每一列都指定关键字 DESC ~
- 无顺序查看方法:
输入▼
#从persons表中检索ages列。
SELECT author,title,price
FROM books;
输出▼
#无顺序查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
+----------------+---------------------------------------------------+-------+
- 倒序查看方法:
输入▼
#倒序从books表中检索多列。
SELECT author,title,price
REOM books
ORDER BY price DESC;
#按照price从大到小输出
输出▼
#顺序查询多列的结果:
+----------------+---------------------------------------------------+-------+
| author | title | price |
+----------------+---------------------------------------------------+-------+
| Thomas Schenk | Caldera OpenLinux system Administration Unleashed | 49.99 |
| Michael Morgan | Java 2 for Professional Developers | 34.99 |
| Pruitt, et al. | Teach Yourself GIMP in 24 Hours | 24.99 |
| Thomas Down | Installing Debian GNU/Linux | 24.99 |
+----------------+---------------------------------------------------+-------+