mySQL Multiple Joins

时间:2015-06-10 10:32:10

标签: php mysql

一直在寻找构建一个mySQL查询,从多个不同的表中选择某些东西。但是,我在运行SQL时遇到错误。不确定我缺少什么

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

错误:

Every derived table must have its own alias

我错过了一些非常明显的东西,这意味着每个表引用必须有自己的Alias,如我的查询中所定义的那样。对我来说,看起来所有的表都有这个,但它仍然无法运行?

我是误解了这个还是看不到什么?

2 个答案:

答案 0 :(得分:3)

你错过了别名:

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

答案 1 :(得分:1)

使用()之外的别名。由于内部查询中只有一个表,因此您可以使用别名tre本身:

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