简体   繁体   中英

Joining two tables together with two foreign keys

I have a Description table which contains certain descriptions along with a unique ID. I have another table that contains two foreign keys to this table. So far i have the following query:

SELECT
Description.description AS Description,
Object.objID AS ID,
Description.description AS Location
FROM
Object
INNER JOIN
Description
ON
Object.objDescID=Description.descID
AND
Object.objLocID=Description.descID;

However this is not working, please can someone point me in the right direction?

If I understand right you want to join to the Description table twice for the same object. Give this a shot and see if it gets you what you're after:

SELECT
  Object.objID AS ID,
  od.description AS Description,
  ld.description AS Location
FROM Object
  INNER JOIN Description AS od
    ON Object.objDescID=od.descID
  INNER JOIN Description AS ld  
    ON Object.objLocID=ld.descID;

Edit: A word of advice, if you allow for null foreign keys you should use a LEFT JOIN instead of an INNER JOIN, that way if one of them is null it doesn't keep the entire record from showing.

Try Running This (might need minor adjustments):

SELECT
Description.description AS Description,
Object.objID AS ID,
Description.description AS Location
FROM
Object
INNER JOIN
Description AS Object.objDescID=Description.descID
INNER JOIN 
Description AS Object.objLocID=Description.descID;

Looks like you need two references to the Description table. Each reference will be joined using one of the foreign key columns.

For example:

SELECT o.objID        AS `ID`
     , d.description  AS `Description`
     , l.description  AS `Location`
FROM Object o
JOIN Description d
  ON d.descID = o.objDescID
JOIN Description l
  ON l.descID = o.objLocID 

We assign the short alias d to the source we get the Description value from.

We assign the short alias l to the source we get the Location value from.

We reference columns from each table using the short alias, rather than the table name.

Essentially, think of the references to the Description table like it's two different tables, even though it's really the same table.

Note that we have to assign an alias to at least one of the references to Description, so that we can distinguish between them. (Otherwise, MySQL won't know which one we're talking about if we just said Description.description .)

Note that if the foreign key column objDescID or objLocID has a NULL value, or a matching value doesn't exist in the referenced table, the query won't return the row from Object.

To ensure you get a row from Object even when the matching values aren't found, you can use an OUTER join operation by including the LEFT keyword.

For example:

SELECT o.objID        AS `ID`
     , d.description  AS `Description`
     , l.description  AS `Location`
FROM Object o
LEFT
JOIN Description d
  ON d.descID = o.objDescID
LEFT
JOIN Description l
  ON l.descID = o.objLocID 

Note that only one alias is actually required, but I tend to assign short aliases to all row sources in a query. This makes the statement more decipherable, and really helps if I later need to add another reference to a table that is already used, or if I need to replace one of the table names with a different table name or an inline view (or subquery), I can leave the alias the same, and change just the rowsource. The other aliases don't make any difference in the actual execution of the statement, they are just there because I follow the same pattern for simple queries that I follow for more complex queries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM