Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining several tables: table specified more than once error

I am attempting to call data after joining all of my tables in a postgreSQL query. I am getting the following error:

Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not Retrieve the result : ERROR:  table name "place" specified more than once
)
Failed to execute SQL chunk

After referring to similar posts and online resources, I have attempted setting aliases (which only create new errors about not referring to the other tables in the FROM clause), reordering the table names (the cleanest, reordered chunk is posted below), and experimenting with UPDATE/FROM/JOIN as an alternative to SELECT/FROM/JOIN. However, I think I ultimately need to be using the SELECT clause.

SELECT *
FROM place

INNER JOIN place ON place.key = form.key
INNER JOIN form ON form.id = items.formid
INNER JOIN items ON items.recid = rec.id
INNER JOIN rec ON rec.id = sub.recid
INNER JOIN sub ON sub.id = type.subid
INNER JOIN type ON type.name = det.typeid;
like image 413
Ryan Avatar asked Sep 11 '25 19:09

Ryan


2 Answers

You have "place" in your query twice, which is allowed but you would have to alias the second use of the table "place". Also you reference something called "det", but it's not a table or alias anywhere in your query. If you want only one place that's fine, just remove the INNER JOIN place and move your place.key = form.key to the form join or to a where clause.

If you want to place tables in their because you are trying to join the table to itself the alias the second one, but you will want to make sure that you then have a clause to join those two tables on (could be part of an on or a where)

like image 171
Michael Robellard Avatar answered Sep 13 '25 08:09

Michael Robellard


The issue ended up being the order of table names. The code below joined everything just fine:

SELECT *
FROM place

INNER JOIN form ON place.key = form.key
INNER JOIN items ON form.id = items.formid
INNER JOIN rec ON items.recid = rec.id
INNER JOIN sub ON rec.id = sub.recid
INNER JOIN type ON sub.id = type.subid
INNER JOIN det ON type.name = det.typeid;
like image 33
Ryan Avatar answered Sep 13 '25 10:09

Ryan