admin 管理员组

文章数量: 1086019


2024年3月9日发(作者:linux创建文件系统的命令)

SELECT 语句 (Microsoft

Access SQL)

适用于: Access 2013 | Office 2013

指示 Microsoft Access 数据库引擎将数据库中的信息作为一组记录返回。

语法

SELECT [谓词] { * |

table.* |[table.]field1 [AS

alias1]

[, [table.]field2 [AS

alias2] [, ...]]} FROM

tableexpression [, ...] [IN

externaldatabase][ ] [ ]

[ ] [ ] [WITH OWNERACCESS OPTION]

SELECT 语句包含以下部分:

部分

说明

下列谓词之一:ALL、DISTINCT、DISTINCTROW 或 TOP。 使用谓词限制返回的记录数。 如果没有指定谓词,则默认值为 ALL。

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

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

字段名,这些字段包含了要检索的数据。

如果包括多个字段,将按它们的排列顺序对其进行检索。

要用作列标题的名称,而不是

表中的原始predicate

*

table

field1,field2

alias1,alias2

部分

说明

列名。

tableexpression

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

包含

tableexpression 中的表(如果表不externaldatabase

在当前数据库中)的数据库的名称。

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

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

SELECT 通常是 SQL 语句中的第一个词。 大多数 SQL 语句都是 SELECT 或 INTO 语句。

SELECT 语句最简化的语法为:

SELECT

fields FROM

table

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

Employees 表中的所有字段。

SELECT * FROM Employees;

如果某个字段名称包含在 FROM 子句的多个表中,请在其前面加上表名和 . (点) 运算符。 在下面的示例中,Department

字段同时存在于 Employees 表和 Supervisors 表中。 SQL

语句从 Employees 表中选择出部门并从 Supervisors 表中选择出主管名:

SELECT ment, me

FROM Employees INNER JOIN Supervisors

WHERE ment = ment;

创建 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 语句中使用其他子句进一步约束和组织所返回的数据。 有关详细信息,请参阅相应子句的帮助主题。

链接提供方:UtterAccess 社区。 UtterAccess 是主要的

Microsoft Access Wiki 和帮助论坛。

示例

下面的一些示例假定 Employees 表中存在一个假想的 Salary

字段。 请注意,该字段实际并不存在于罗斯文数据库的

Employees 表中。

This example creates a dynaset-type Recordset based on

an SQL statement that selects the LastName and

FirstName fields of all records in the Employees table.

它调用 EnumFields 过程,该过程将 Recordset 对象的内容显示到调试窗口。

Sub SelectX1()

Dim dbs As Database, rst As Recordset

' Modify this line to include the path to

Northwind

' on your puter.

Set dbs = OpenDatabase("")

' Select the last name and first name values

of all

' records in the Employees table.

Set rst = cordset("SELECT LastName,

" _

& "FirstName FROM Employees;")

' Populate the recordset.

st

' Call EnumFields to print the contents of the

' Recordset.

EnumFields rst,12

End Sub

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

Sub SelectX2()

Dim dbs As Database, rst As Recordset

' Modify this line to include the path to

Northwind

' on your puter.

Set dbs = OpenDatabase("")

' Count the number of records with a

PostalCode

' value and return the total in the Tally

field.

Set rst = cordset("SELECT Count " _

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

' Populate the Recordset.

st

' Call EnumFields to print the contents of

' the Recordset. Specify field width = 12.

EnumFields rst, 12

End Sub

以下示例显示了雇员人数以及平均和最高工资。

Sub SelectX3()

Dim dbs As Database, rst As Recordset

' Modify this line to include the path to

Northwind

' on your puter.

Set dbs = OpenDatabase("")

' Count the number of employees, calculate the

' average salary, and return the highest

salary.

Set rst = cordset("SELECT Count (*)

" _

& "AS TotalEmployees, Avg(Salary) " _

& "AS AverageSalary, Max(Salary) " _

& "AS MaximumSalary FROM Employees;")

' Populate the Recordset.

st

' Call EnumFields to print the contents of

' the Recordset. Pass the Recordset object and

' desired field width.

EnumFields rst, 17

End Sub

The Sub procedure EnumFields is passed a Recordset

object from the calling procedure. The procedure then

formats and prints the fields of the Recordset to the

Debug window. The variable is the desired printed

field width. Some fields may be truncated.

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 = Count

' Set the lngFields variable to the number of

' fields in the Recordset.

lngFields =

"There are " & lngRecords _

& " records containing " & lngFields _

& " fields in the recordset."

' Form a string to print the column heading.

strTitle = "Record "

For lngFldCount = 0 To lngFields - 1

strTitle = strTitle _

& Left((lngFldCount).Name _

& Space(intFldLen), intFldLen)

Next lngFldCount

' Print the column heading.

strTitle

' Loop through the Recordset; print the record

' number and field values.

rst

For lngRecCount = 0 To lngRecords - 1

Right(Space(6) & _

Str(lngRecCount), 6) & " ";

For lngFldCount = 0 To lngFields - 1

' Check for Null values.

If IsNull((lngFldCount))

Then

strTemp = ""

Else

' Set strTemp to the field

contents.

Select Case _

(lngFldCount).Type

Case 11

strTemp = ""

Case dbText, dbMemo

strTemp = _

(lngFldCount)

Case Else

strTemp = _

str((lngFldCount))

End Select

End If

Left(strTemp _

& Space(intFldLen), intFldLen);

Next lngFldCount

xt

Next lngRecCount

End Sub


本文标签: 使用 返回 数据库