当前位置: 首页 > 网络学院 > 服务端脚本教程 > SQL > SQL Select

SQL
SQL Create View
SQL Server
SQL 速查
SQL 摘要
MYSQL 列类型存储需求

SQL Select


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 1050 ::
收藏到网摘: n/a

The SQL SELECT Statement
SQL的SELECT语句

The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set).
SELECT语句可用于从一中数据表中选择所需的数据。结果以表格的形式存放在一起(可称为结果集)

Syntax
语法

SELECT column_name(s)
FROM table_name

Note: SQL statements are not case sensitive. SELECT is the same as select.
注意:SQL语句并不区分大小写。SELECT和select效果一样


SQL SELECT Example
SQL的SELECT举例

To select the content of columns named "LastName" and "FirstName", from the database table called "Persons", use a SELECT statement like this:
想从一个名为"Persons"的数据表中选出名为"LastName"以及"FirstName"的字段内容的话,可以用这段SELECT语句办到:

SELECT LastName,FirstName FROM Persons

数据表 "Persons":

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

结果

LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari

 


Select All Columns
选择全部栏目内容

To select all columns from the "Persons" table, use a * symbol instead of column names, like this:
想要一次性将所有字段的内容显示出来可以使用 * 这个符号来替代所有字段名:

SELECT * FROM Persons

结果

LastName FirstName Address City
Hansen Ola Timoteivn 10 Sandnes
Svendson Tove Borgvn 23 Sandnes
Pettersen Kari Storgt 20 Stavanger

 


The Result Set
结果集

The result from a SQL query is stored in a result-set. Most database software systems allow navigation of the result set with programming functions, like: Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, etc.
从SQL查询后所返回的结果都是存放在result-set(结果集)中的。大多数数据库系统都是可以通过程序函数来对结果集中的内容进行导航的,比方说:Move-To-First-Record(移动到第一条记录),Get-Record-Content(获取记录内容),Move-To-Next-Record(移动到下一条记录),等等

Programming functions like these are not a part of this tutorial. To learn about accessing data with function calls, please visit our ADO tutorial.
这方面的程序函数并不是本教程里的内容,如果要学习如何通过调用函数来访问这些数据的话,可以访问我们的ADO教程


Semicolon after SQL Statements?
在SQL声明后需要分号?

Semicolon is the standard way to separate each SQL statement in database systems that allow more than one SQL statement to be executed in the same call to the server.
在数据库系统中可以通过使用标准的分号隔开法实现一句SQL声明执行多个操作

Some SQL tutorials end each SQL statement with a semicolon. Is this necessary? We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL statement, but some database programs force you to use it.
在一些SQL教程中每一个SQL声明后都带有分号,这是不是必须的呢?我们所使用的MS Access数据库和SQL Server2000数据库不需要使用分号来隔开每条SQL声明,但有些数据库程序却必须这么做


The SELECT DISTINCT Statement
SELECT DISTINCT声明

The DISTINCT keyword is used to return only distinct (different) values.
关键字DISTINCT可以用来返回不同的值(无重复)

The SELECT statement returns information from table columns. But what if we only want to select distinct elements?
SELECT声明可返回来自数据表栏目内的信息,但当我们只需要返回那些不重复的元素时该怎么做?

With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement:
使用SQL,我们所要做的就是给SELECT声明再加上一个DISTINCT关键字

语法

SELECT DISTINCT column_name(s)
FROM table_name

 


Using the DISTINCT keyword
使用DISTINCT关键字

To select ALL values from the column named "Company" we use a SELECT statement like this:
要选择栏目"Company"中所有的值,那么我们可以使用这样一条SELECT声明:

SELECT Company FROM Orders

"Orders" 表

Company OrderNumber
Sega 3412
RuanChen 2312
Trio 4678
RuanChen 6798

结果

Company
Sega
RuanChen
Trio
RuanChen

Note that "RuanChen" is listed twice in the result-set.
可以注意到在结果集中出现了两次。

To select only DIFFERENT values from the column named "Company" we use a SELECT DISTINCT statement like this:
要选出没有重复的值,我们就得使用SELECT DISTINCT 声明:

SELECT DISTINCT Company FROM Orders

结果:

Company
Sega
RuanChen
Trio

Now "RuanChen" is listed only once in the result-set.
现在"RuanChen"在结果集中只出现了一次

评论 (1) 1 All

登陆 | 还没注册?