Legend1989
Legend1989

Reputation: 664

mySQL Multiple Joins

Been looking to build a mySQL query that selects certain things from a number of different tables. However I am encountering an error on running the SQL. Not sure what I am missing

The SQL:

SELECT * 
FROM tripreport as tr
JOIN 
(
   SELECT * FROM tripreportentries as tre LIMIT 1 
) ON tr.tripreportid = tre.reportid 
JOIN users as u ON tr.userid = u.id
JOIN user_details as ud ON u.id = ud.userid

The error:

Every derived table must have its own alias

Am I missing something extremely obvious, as far this means each table reference must have its own Alias as defined with the as part of my query. To me it looks like all the tables have this, but it is still failing to run?

Am I misunderstanding this or over looking something?

Upvotes: 0

Views: 85

Answers (2)

Hotdin Gurning
Hotdin Gurning

Reputation: 1819

You missed alias:

....
JOIN 
(
   SELECT * FROM tripreportentries LIMIT 1 
) AS tre ON
...

Upvotes: 3

Raging Bull
Raging Bull

Reputation: 18737

Use the alias name out of (). Since you have only one table in inner query, you can use the alias tre itself:

SELECT * 
FROM tripreport as tr
JOIN 
(
   SELECT * FROM tripreportentries LIMIT 1 
) as tre ON tr.tripreportid = tre.reportid 
JOIN users as u ON tr.userid = u.id
JOIN user_details as ud ON u.id = ud.userid

Upvotes: 1

Related Questions