Compartir a través de


How to: Query Multiple Tables and Views

When you need access to information stored in two or more tables, add all the needed tables to the query or view and join them on common fields. You can then search the records in all the tables for the information you need. You can use any combination of database tables, free tables, and local or remote views in your queries.

The term "view" can refer to either an input source or an object you create in the View Designer; therefore, the term "table" is used for both tables and views used as input sources.

Adding Views and Tables to a Query

When you add tables or views to your query, Visual FoxPro suggests a possible join between the tables or views, based on matching field names.

For example, if you add the Customer table in the Visual FoxPro ...\Samples\Data directory to the Query Designer, and then add the Orders table, Visual FoxPro suggests a join between the tables based on the matching fields Customer.cust_id and Orders.cust_id.

If you are using a database that contains persistent relationships defined between the views or tables within it, Visual FoxPro uses the existing relationships as default joins.

If you want to add database tables or views to your query, you may need to open the appropriate database to make them available.

To add a table or view to a query

  1. In the Query Designer toolbar, choose Add Table.

  2. In the Add Table or View dialog box, select the database you want to use, choose Tables or Views, select the table or view you want to add, and choose OK.

    -or-

    If you want to add a table that is not part of the database, choose Other, locate the table in the Open dialog box, and choose OK.

  3. In the Join Condition dialog box, verify the suggested join and choose OK.

    If Visual FoxPro cannot suggest a likely match between fields, select the matching fields in the Join Condition dialog box yourself.

See Also

Tasks

How to: Control Record Selection with Joins

How to: Customize Queries

How to: Create Queries (Visual FoxPro)

How to: Run Queries (Visual FoxPro)

Reference

Add Table or View Dialog Box

Join Condition Dialog Box