Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录

第二课 检索、找回数据(Retrieving Data)

本课学习使用SELECT来从数据库表中检索一列或者多列数据。

2.1 SELECT声明

其目的是从一张或多张表中检索找回信息。

明确两点:要检索找回什么?从哪里检索找回?

2.2 检索找回单列(Retrieving Individual Columns)

输入:

SELECT prod_name
FROM Products;

Tips:
未排序数据(Unsorted Data)
; 声明结束
SQL声明和例子(不区分大小写)
忽略剩余空间

2.3 检索找回多列(Retrieving Multiple Columns)

输入:

SELECT prod_id;prod_name;prod_price
FROM Products;

Tips:
数据显示

2.4 检索找回所有列(Retrieving All Columns)

输入:

SELECT *
FROM Products;

Tips:
使用通配符
检索找回不确定的列

2.5 检索找回不同的行(Retrieving Distinct Rows)

输入:(检索找回所有的行)

SELECT vend_id
FROM Products;

输入:(检索找回不同的行)

SELECT DISTINCT vend_id
FROM Products;

2.6 限制结果(Limiting Results)

<不同的SQL实现不同>

对于MySQL,使用LIMIT语句:
输入:

SELECT prod_name
FROM Products
LIMIT 5;

如果要返回下5行呢?

SELECT prod_name
FROM Products
LIMIT 5 OFFSET 5;
注意:

row0:第一个获取的是row0,不是row1.LIMIT 1 OFFSET 1获取的是第二行。
MySQL快捷方式:
LIMIT 4 OFFSET 3
等价于:
LIMIT 3,4

2.7 使用注释(Using Comments )

1、
SELECT prod_name -- this is a comment
FROM Products;

2、
# This is a comment
SELECT prod_name
FROM Products;

3、
/* SELECT prod_name, vend_id
FROM Products;*/
SELECT prod_name
FROM Products;

2.8 总结

In this lesson, you learned how to use the SQL SELECT statement to retrieve a single table column, multiple table columns, and all table columns. You also learned how to return distinct values and how to comment your code. And unfortunately, you were also introduced to the fact that more complex SQL tends to be less portable SQL. Next you’ll learn how to sort the retrieved data.

你可能感兴趣的:(Sams Teach Yourself SQL in 10 Minutes(Fourth Edition)学习记录)