SELECT ステートメント (Microsoft Access SQL)

適用先: Access 2013 | Office 2013

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 ALL、DISTINCT、DISTINCTROW、TOP のいずれかの述語。 述語を使用して、返されるレコードの数を制限します。 指定がない場合は、ALL になります。
* 指定したテーブル (複数可) のすべてのフィールドを選択するよう指定します。
table レコードを選択するフィールドを含んだテーブルの名前。
field1、field2 取得するデータのある 1 つ以上のフィールドの名前。 複数のフィールドを指定した場合は、指定順に取得されます。
alias1、alias2 テーブル内の元の列名の代わりに列ヘッダーとして使用する名前。
tableexpression 取得するデータを含んだテーブル (複数可) の名前。
externaldatabase テーブルが現在のデータベースにない場合は、 tableexpression 内のテーブルを含むデータベースの名前。

注釈

この操作を実行するため、Microsoft Jet データベース エンジンは指定されたテーブル (複数可) を検索して、選択した列を抽出し、条件に一致する行を選択して、結果行を指定の順序に並べ替えたり、グループ化したりします。

SELECT ステートメントはデータベース内のデータを変更することはありません。

通常、SELECT は SQL ステートメントの先頭に記述します。 ほとんどの SQL ステートメントは SELECT ステートメントまたは SELECT...INTO ステートメントのいずれかになります。

SELECT ステートメントの最も簡単な構文を次に示します。

SELECT fields FROM table

アスタリスク (*) を使用すると、テーブル内のすべてのフィールドが選択できます。 次の例では、Employees テーブルのすべてのフィールドを選択します。

SELECT * FROM Employees;

FROM 句の複数のテーブルにフィールド名が含まれている場合は、その前にテーブル名と . (ドット) 演算子を付けます。 In the following example, the Department field is in both the Employees table and the Supervisors table. The SQL statement selects departments from the Employees table and supervisor names from the Supervisors table:

SELECT Employees.Department, Supervisors.SupvName 
FROM Employees INNER JOIN Supervisors 
WHERE Employees.Department = Supervisors.Department;

When a Recordset object is created, the Microsoft Jet database engine uses the table's field name as the Field object name in the Recordset object. If you want a different field name or a name is not implied by the expression used to generate the field, use the AS reserved word. The following example uses the title Birth to name the returned Field object in the resulting Recordset object:

SELECT BirthDate 
AS Birth FROM Employees;

Whenever you use aggregate functions or queries that return ambiguous or duplicate Field object names, you must use the AS clause to provide an alternate name for the Field object. The following example uses the title HeadCount to name the returned Field object in the resulting Recordset object:

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. It calls the EnumFields procedure, which prints the contents of a Recordset object to the Debug window.

    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 フィールドを指定します。

    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 

次の使用例では、社員数と平均給与額および最高給与額を表示します。

    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 のフィールドがフォーマットされ、 Debug ウィンドウに出力されます。 変数は、必要な出力フィールド幅を示します。 一部のフィールドが表示されない場合もあります。

    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