Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > Access数据库-查询/SQL语句

全面掌握MS ACCESS SQL(19)

时 间:2018-01-08 17:33:52
作 者:Big Young   ID:252  城市:襄阳
摘 要:    SELECT语句的基本语法.
正 文:

第五章 运用Select(选择)语句

第一节 Select语句的基本语法

Select 语句是MS ACCESS SQL中的最重要的一条命令语句,它指示Microsoft Access数据库引擎将数据库中的信息作为一组记录返回给用户。

一、完整的语法

Select [predicate] { * | table.* | [table.]field1 [AS alias1] [, [table.]field2 [AS alias2] [, ...]]} FROM tableexpression [, ...] [IN externaldatabase] [Where... ] [GROUP BY... ] [HAVING... ] [ORDER BY... ] [WITH OWNERACCESS OPTION]

Select语句包含项目部分的说明:

项目

说明

Predicate

下列谓词之一:ALLDISTINCTDISTINCTROWTOP。可以使用谓词来限定返回记录的数量。如果没有指定谓词,则默认值为ALL

*

指定选择指定表中的所有字段。

Table

表的名称,该表包含从中选择记录的字段。

field1field2

字段名,这些字段包含了要检索的数据。如果包括多个字段,将按它们的排列顺序对其进行检索。

alias1alias2

用作列标题的别名,不是table中的原始列名。

Tableexpression

表名称,其中包含要检索的数据。

Externaldatabase

如果tableexpression中的表不在当前数据库中,则使用该参数指定该数据库名。

二、相关注释说明

若要执行此项操作,Microsoft Jet数据库引擎会搜索指定的表,并提取选定的列,再选择符合条件的行,然后按指定的顺序对得到的行进行排序或分组。

Select语句不会更改数据库中的数据。

Select通常是此选择查询SQL语句中的第一个词。大多数SQL语句都是SelectSelect...INTO语句。

Select 语句最简化的语法为:

Select fields FROM table

可以通过星号 (*) 来选择表中所有的字段。以下的示例选择在Employees表中的所有字段:

Select * FROM Employees;

如果一个字段名包括于FROM子句内的多个表中,请在该字段前面加上表名和 .(圆点)号。在下面的示例中,Department字段同时存在于Employees表和Supervisors表中。SQL语句从Employees表中选择出部门并从Supervisors表中选择出主管名:

Select Employees.Department, Supervisors.SupvName

FROM Employees INNER JOIN Supervisors

Where Employees.Department = Supervisors.Department;

创建Recordset对象时,Microsoft Jet数据库引擎将使用表的字段名作为Recordset对象中的Field对象名。如果需要其他字段名或者名称不适合用来生成该字段的表达式,请使用AS保留字。以下示例使用标题Birth来命名生成的Recordset对象中的返回Field对象:

Select BirthDate

AS Birth FROM Employees;

只要使用的聚合函数或查询返回的是不明确的或重复的Field对象名称,就必须使用AS子句为该Field对象另外提供一个替代名称。下面的示例使用标题HeadCount来命名在结果Recordset对象中的返回Field对象:

Select COUNT(EmployeeID)

AS HeadCount FROM Employees;

可以在Select语句中使用其他子句进一步约束和组织所返回的数据。有关详细信息,请参阅相应子句的帮助主题。

三、几个示例

下面的一些示例假定Employees表中存在一个假想的Salary字段。请注意,该字段实际并不存在于罗斯文数据库的Employees表中。

本例基于SQL语句创建一个动态集类型的Recordset,该语句选择 Employees 表中所有记录的 LastName FirstName 字段。它调用 EnumFields 过程,该过程将 Recordset 对象的内容显示到调试窗口。

VBA

Sub SelectX1()

 

    Dim dbs As Database, rst As Recordset

 

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

 

    ' Select the last name and first name values of all 

    ' records in the Employees table.

    Set rst = dbs.OpenRecordset("Select LastName, " _

        & "FirstName FROM Employees;")

 

    ' Populate the recordset.

    rst.MoveLast

 

    ' Call EnumFields to print the contents of the

    ' Recordset.

    EnumFields rst,12

 

    dbs.Close

 

End Sub

以下示例计算 PostalCode 字段中有条目的记录数,并将返回的字段命名为 Tally

VBA

Sub SelectX2()

 

    Dim dbs As Database, rst As Recordset

 

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

 

    ' Count the number of records with a PostalCode 

    ' value and return the total in the Tally field.

    Set rst = dbs.OpenRecordset("Select Count " _

        & "(PostalCode) AS Tally FROM Customers;")

 

    ' Populate the Recordset.

    rst.MoveLast

 

    ' Call EnumFields to print the contents of 

    ' the Recordset. Specify field width = 12.

    EnumFields rst, 12

 

    dbs.Close

 

End Sub

 

以下示例显示雇员数以及平均薪水和最高薪水。

VBA

Sub SelectX3()

 

    Dim dbs As Database, rst As Recordset

 

    ' Modify this line to include the path to Northwind

    ' on your computer.

    Set dbs = OpenDatabase("Northwind.mdb")

 

    ' Count the number of employees, calculate the 

    ' average salary, and return the highest salary.

    Set rst = dbs.OpenRecordset("Select Count (*) " _

        & "AS TotalEmployees, Avg(Salary) " _

        & "AS AverageSalary, Max(Salary) " _

        & "AS MaximumSalary FROM Employees;")

 

    ' Populate the Recordset.

    rst.MoveLast

 

    ' Call EnumFields to print the contents of

    ' the Recordset. Pass the Recordset object and

    ' desired field width.

    EnumFields rst, 17

 

    dbs.Close

 

End Sub

 

调用过程向 Sub 过程 EnumFields 传递了一个 Recordset 对象。然后该过程设置 Recordset 字段的格式并将这些字段显示到"调试"窗口。该变量是需要的显示的字段宽度。某些字段可能会被截断。

VBA

Sub EnumFields(rst As Recordset, intFldLen As Integer)

 

    Dim lngRecords As Long, lngFields As Long

    Dim lngRecCount As Long, lngFldCount As Long

    Dim strTitle As String, strTemp As String

 

    ' Set the lngRecords variable to the number of

    ' records in the Recordset.

    lngRecords = rst.RecordCount

 

    ' Set the lngFields variable to the number of

    ' fields in the Recordset.

    lngFields = rst.Fields.Count

 

    Debug.Print "There are " & lngRecords _

        & " records containing " & lngFields _

        & " fields in the recordset."

    Debug.Print

 

    ' Form a string to print the column heading.

    strTitle = "Record  "

    For lngFldCount = 0 To lngFields - 1

        strTitle = strTitle _

        & Left(rst.Fields(lngFldCount).Name _

        & Space(intFldLen), intFldLen)

    Next lngFldCount    

 

    ' Print the column heading.

    Debug.Print strTitle

    Debug.Print

 

    ' Loop through the Recordset; print the record

    ' number and field values.

    rst.MoveFirst

 

    For lngRecCount = 0 To lngRecords - 1

        Debug.Print Right(Space(6) & _

            Str(lngRecCount), 6) & "  ";

 

        For lngFldCount = 0 To lngFields - 1

            ' Check for Null values.

            If IsNull(rst.Fields(lngFldCount)) Then

                strTemp = "<null>"

            Else

                ' Set strTemp to the field contents. 

                Select Case _

                    rst.Fields(lngFldCount).Type

                    Case 11

                        strTemp = ""

                    Case dbText, dbMemo

                        strTemp = _

                            rst.Fields(lngFldCount)

                    Case Else

                        strTemp = _

                            str(rst.Fields(lngFldCount))

                End Select

            End If

 

            Debug.Print Left(strTemp _ 

                & Space(intFldLen), intFldLen);

        Next lngFldCount

 

        Debug.Print

 

        rst.MoveNext

 

    Next lngRecCount

 

End Sub



Access软件网QQ交流群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助