How to get matching data from another SQL table for two different columns: Inner Join and/or Union?

Levi Tonet

I've got two tables in MS Access that keep track of class facilitators and the classes they facilitate. The two tables are structured as follows:

tbl_facilitators

facilID -> a unique autonumber to keep track of individual teachers
facilLname -> the Last name of the facilitator
facilFname -> the First name of the facilitator

tbl_facilitatorClasses

classID -> a unique autonumber to keep track of individual classes
className -> the name of the class (science, math, etc)
primeFacil -> the facilID from the first table of a teacher who is primary facilitator
secondFacil -> the facilID  from the first table of another teacher who is backup facilitator

I cannot figure out how to write an Inner Join that pulls up the results in this format:

Column 1:  Class Name
Column 2:  Primary Facilitator's Last Name
Column 3:  Primary Facilitator's First Name
Column 4:  Secondary Facilitator's Last Name
Column 5:  Secondary Facilitator's First Name

I am able to pull up and get the correct results if I only request the primary facilitator by itself or only request the secondary facilitator by itself. I cannot get them both to work out, though.

This is my working Inner Join:

SELECT tbl_facilitatorClasses.className,
    tbl_facilitators.facilLname, tbl_facilitators.facilFname
FROM tbl_facilitatorClasses
INNER JOIN tbl_facilitators
ON tbl_facilitatorClasses.primeFacil = tbl_facilitators.facilID;

Out of desperation I also tried a Union, but it didn't work out as I had hoped. Your help is greatly appreciated. I'm really struggling to make any progress at this point. I don't often work with SQL.

SOLUTION

Thanks to @philipxy I came up with the following query which ended up working:

SELECT tblCLS.className,
    tblP.facilLname, tblP.facilFname, tblS.facilLname, tblS.facilFname
FROM (tbl_facilitatorClasses AS tblCLS
INNER JOIN tbl_facilitators AS tblP
ON tblCLS.primeFacil=tblP.facilID)
INNER JOIN tbl_facilitators AS tblS
ON tblCLS.secondFacil=tblS.facilID;

When performing multiple Inner Joins in MS Access, parenthesis are needed...As described in this other post.

philipxy

Every base table has a statement template, aka predicate, parameterized by column names, by which we put a row in or leave it out. We can use a shorthand for the predicate that is like its SQL declaration.

// facilitator [facilID] is named [facilFname] [facilLname]
facilitator(facilID, facilLname, facilFname)
// class [classID] named [className] has prime [primeFacil] & backup [secondFacil]
class(classID, className, primeFacil, secondFacil)

Plugging a row into a predicate gives a statement aka proposition. The rows that make a true proposition go in a table and the rows that make a false proposition stay out. (So a table states the proposition of each present row and states NOT the proposition of each absent row.)

// facilitator f1 is named Jane Doe
facilitator(f1, 'Jane', 'Doe')
// class c1 named CSC101 has prime f1 & backup f8
class(c1, 'CSC101', f1, f8)

But every table expression value has a predicate per its expression. SQL is designed so that if tables T and U hold the (NULL-free non-duplicate) rows where T(...) and U(...) (respectively) then:

  • T CROSS JOIN U holds rows where T(...) AND U(...)
  • T INNER JOIN U ONcondition holds rows where T(...) AND U(...) AND condition
  • T LEFT JOIN U ONcondition holds rows where (for U-only columns U1,...)
        T(...) AND U(...) AND condition
    OR T(...)
        AND NOT there EXISTS values for U1,... where [U(...) AND condition]
        AND U1 IS NULL AND ...
  • T WHEREcondition holds rows where T(...) AND condition
  • T INTERSECT U holds rows where T(...) AND U(...)
  • T UNION U holds rows where T(...) OR U(...)
  • T EXCEPT U holds rows where T(...) AND NOT U(...)
  • SELECT DISTINCT * FROM T holds rows where T(...)
  • SELECT DISTINCTcolumns to keepFROM T holds rows where
    there EXISTS values for columns to drop where T(...)
  • VALUES (C1, C2, ...)((v1,v2, ...), ...) holds rows where
    C1 = v1 AND C2 = v2 AND ... OR ...

Also:

  • (...) IN T means T(...)
  • scalar= T means T(scalar)
  • T(..., X, ...) AND X = Y means T(..., Y, ...) AND X = Y

So to query we find a way of phrasing the predicate for the rows that we want in natural language using base table predicates, then in shorthand using base table predicates, then in SQL using base table names (plus conditions wherever needed). If we need to mention a table twice then we give it aliases.

// natural language
there EXISTS values for classID, primeFacil & secondFacil where
    class [classID] named [className] has prime [primeFacil] & backup [secondFacil]
AND facilitator [primeFacil] is named [pf.facilFname] [pf.facilLname]
AND facilitator [secondFacil] is named [sf.facilFname] [sf.facilLname]

// shorthand
there EXISTS values for classID, primeFacil & secondFacil where
    class(classID,className, primeFacil, secondFacil)
AND facilitator(pf.facilID, pf.facilLname, pf.facilFname)
AND pf.facilID = primeFacil
AND facilitator(sf.facilID, sf.facilLname, sf.facilFname)
AND sf.facilID = secondFacil

// table names & (MS Access) SQL
SELECT className, pf.facilLname, pf.facilFname, sf.facilLname, sf.facilFname
FROM (class JOIN facilitator AS pf ON pf.facilID = primeFacil)
JOIN facilitator AS sf ON sf.facilID = secondFacil

OUTER JOIN would be used when a class doesn't always have both facilitators or something doesn't always have all names. (Ie if a column can be NULL.) But you haven't given the specific predicates for your base table and query or the business rules about when things might be NULL so I have assumed no NULLs.

(Re MS Access JOIN parentheses see this from SO and this from MS.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL select data from table and inner join the union all selection from another two identical tables

From Dev

how fetch data from two table in codeigniter by using inner join

From Dev

How to INNER JOIN conditional data from two tables with SQL

From Dev

SQL How to Delete Data from Table Using INNER JOIN

From Dev

show different columns from different table with inner join

From Dev

How to calculate the ratio of records based on values of two columns distinctly with the inner join of another table?

From Dev

How to get value from one table column when two columns of the same name exist in an sql join

From Dev

Fetch data from two SQL tables with INNER JOIN, display HTML table

From Dev

Inner join no matching columns

From Dev

Getting error when I'm trying to get data from 3 table using sql INNER JOIN query

From Dev

How to join two columns in a table with a primary key in another table?

From Dev

How to join two tables where Columns are not matching in SQL

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

How to get count of two fields from two different table with grouping a field from another table in mysql

From Dev

How to make inner join two times to single table in sql server

From Dev

How to select matching records in two different columns in same table?

From Dev

MYSQL - Get data from INNER JOIN with two tables

From Dev

SQL query to do INNER JOIN from two tables of a data base

From Dev

how two get data from 2 different table c#

From Dev

How is inner join different from normal multi-table selection?

From Dev

How to inner join from another table and order by specific row value?

From Dev

SQL Query to compare two columns with one column from another table (and get two values)

From Dev

Matching two columns of two different data frames

From Dev

How to get matching and non matching rows from a SQL Join

From Dev

INNER JOIN to view detail another table SQL

From Dev

How I get common values from two query in a table and join it with another table in MySql?

From Dev

Get Common data from two different table

From Dev

Get Common data from two different table

From Dev

SQL union of two tables with different columns

Related Related

  1. 1

    MySQL select data from table and inner join the union all selection from another two identical tables

  2. 2

    how fetch data from two table in codeigniter by using inner join

  3. 3

    How to INNER JOIN conditional data from two tables with SQL

  4. 4

    SQL How to Delete Data from Table Using INNER JOIN

  5. 5

    show different columns from different table with inner join

  6. 6

    How to calculate the ratio of records based on values of two columns distinctly with the inner join of another table?

  7. 7

    How to get value from one table column when two columns of the same name exist in an sql join

  8. 8

    Fetch data from two SQL tables with INNER JOIN, display HTML table

  9. 9

    Inner join no matching columns

  10. 10

    Getting error when I'm trying to get data from 3 table using sql INNER JOIN query

  11. 11

    How to join two columns in a table with a primary key in another table?

  12. 12

    How to join two tables where Columns are not matching in SQL

  13. 13

    How to get count of two fields from two different table with grouping a field from another table in mysql

  14. 14

    How to get count of two fields from two different table with grouping a field from another table in mysql

  15. 15

    How to make inner join two times to single table in sql server

  16. 16

    How to select matching records in two different columns in same table?

  17. 17

    MYSQL - Get data from INNER JOIN with two tables

  18. 18

    SQL query to do INNER JOIN from two tables of a data base

  19. 19

    how two get data from 2 different table c#

  20. 20

    How is inner join different from normal multi-table selection?

  21. 21

    How to inner join from another table and order by specific row value?

  22. 22

    SQL Query to compare two columns with one column from another table (and get two values)

  23. 23

    Matching two columns of two different data frames

  24. 24

    How to get matching and non matching rows from a SQL Join

  25. 25

    INNER JOIN to view detail another table SQL

  26. 26

    How I get common values from two query in a table and join it with another table in MySql?

  27. 27

    Get Common data from two different table

  28. 28

    Get Common data from two different table

  29. 29

    SQL union of two tables with different columns

HotTag

Archive