SQL statement from two tables

I would like to know if it is possible to select specific columns from one table and another column from the second table that will refer to the non-imported column in the first table. I have to get this data from access and don't know if this is possible with Access or SQL at all.

+1


source to share


4 answers


Assuming the following table structure:

CREATE TABLE tbl_1 (
    pk_1 int,
    field_1 varchar(25),
    field_2 varchar(25)
);

CREATE TABLE tbl_2 (
    pk_2 int,
    fk_1 int,
    field_3 varchar(25),
    field_4 varchar(25)
);

      

You can use the following:

SELECT t1.field_1, t2.field_3
FROM tbl_1 t1
INNER JOIN tbl_2 t2 ON t1.pk_1 = t2.fk_1
WHERE t2.field_3 = "Some String"

      



Regarding Bill's post, there are two ways to create JOINs in SQL queries:

  • Implicitly. The join is created using a WHERE clause of a query with multiple tables specified in the FROM clause

  • Obviously. The connection is created using the appropriate type of JOIN clause (INNER, LEFT, RIGHT, FULL)

It is always recommended to use the explicit JOIN syntax, as implicit joins can present problems as the query becomes more complex.

For example, if you later add an explicit join to a query that already uses an implicit join with multiple tables in the FROM clause, the first table referenced in the FROM clause will not be visible to the explicitly linked table.

+6


source


What you are looking for is JOINs:

http://en.wikipedia.org/wiki/Join_(SQL)



You need primary keys for referenced datasets and foreign keys in the first table.

+2


source


I'm not 100% sure, I understand your question.

Is as follows:

Your first table is being imported from somewhere else. You are only importing a few columns. You want to create a query that refers to a column that you did not import.

If so, it is simply not possible. As for the Access query engine for unimported columns, they don't exist.

Why not just import them?

0


source


But primary keys make the query more efficient

0


source







All Articles