子查询的SQL别名

时间:2013-02-20 18:16:39

标签: mysql sql yii

我遇到以下问题的问题:

SELECT * FROM (
  (SELECT * FROM Images 
   WHERE create_user_id=:user_id) RIGHT INNER JOIN 
  (SELECT * FROM PhotoGallery) ON id=centity_id
 )
 ORDER BY centity_id;

我得到1248 - 每个派生表必须有自己的别名错误,我知道我需要给那些表别名,但无论我做什么,我总是得到错误。谁能帮我解决这个问题?非常感谢你!

4 个答案:

答案 0 :(得分:0)

试试这个

SELECT * 
  FROM Images i RIGHT JOIN
       PhotoGallery p ON i.id=p.centity_id
 WHERE i.create_user_id=:user_id
 ORDER BY p.centity_id

根据您的DDL和所需的输出联接可能是RIGHTINNERLEFT,但不是RIGHT INNER JOIN

答案 1 :(得分:0)

您的子查询需要别名。类似的东西:

SELECT * FROM (
  (SELECT *
   FROM Images 
   WHERE create_user_id=:user_id
  ) i RIGHT INNER JOIN 
  PhotoGallery pg
  ON i.id=pg.centity_id
 )
 ORDER BY centity_id;

答案 2 :(得分:0)

我从来没有听说过支持“RIGHT INNER JOIN”的dbms。选择

之一
  • INNER JOIN,或
  • RIGHT OUTER JOIN,或
  • RIGHT JOIN(与RIGHT OUTER JOIN相同)。

答案 3 :(得分:0)

如果您正在执行select *,则无需进行子查询。

SELECT * FROM Images 
  RIGHT OUTER JOIN PhotoGallery ON id=centity_id
WHERE create_user_id = :user_id
ORDER BY centity_id;