简体   繁体   English

命名临时表上的SQL语法错误

[英]SQL Syntax error on named temp-table

Could anyone give me a hint, why the following query throws a syntax error? 谁能给我一个提示,为什么以下查询引发语法错误?

SELECT *  
  FROM 
     (
       ( SELECT cu.ID id
              , name
              , ego_id
              , u.fb_id 
           FROM contact_users cu 
           JOIN users u 
             ON cu.ego_id = u.ID
       ) temp 
    LEFT 
    JOIN fb_user 
      ON temp.fb_id = fb_user.user_fb_id
     ) T4

ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds 
to your MySQL server version for the right syntax to use near 'T4'...

If I remove T4 everthing is fine. 如果我删除T4一切都很好。

I am using MySQL 5.5.47 我正在使用MySQL 5.5.47

Why the following query throws a syntax error? 为什么以下查询引发语法错误?

Because you can't alias an inline view if one doesn't exist. 因为如果一个内联视图不存在,则无法为它命名。

When you remove T4, the engine just see's ()'s for order of operation. 卸下T4时,引擎只会看到()的操作顺序。 When you add the alias it assumes a well formed inline view exists and thus looks for the proper syntax (At least SELECT and FROM) which do not exist. 当您添加别名时,它假定存在格式正确的内联视图,因此将查找不存在的正确语法(至少是SELECT和FROM)。

Put simply: 简单地说:

The alias T4 has no select or from; 别名T4没有选择或从; to resolve this: 解决此问题:

  • add them 添加他们
  • remove the initial ( and the Ending ) T4 (they are not needed) 删除初始(和结尾) T4 (不需要)
  • refactor the SQL (why the inline view in the first place? it's not needed.) 重构SQL(为什么首先要有内联视图?不需要它。)

MORE DETAIL 更多详情

Your Temp alias works because it is a complete and accurate query that can run on it's own. 您的Temp别名之所以有效,是因为它是一个完整而准确的查询,可以单独运行。

SELECT cu.ID id
              , name
              , ego_id
              , u.fb_id 
           FROM contact_users cu 
           JOIN users u 
             ON cu.ego_id = u.ID

WHERE AS: 在哪里:

( SELECT cu.ID id
          , name
          , ego_id
          , u.fb_id 
  FROM contact_users cu 
  JOIN users u 
    ON cu.ego_id = u.ID
) temp 
LEFT 
JOIN fb_user 
  ON temp.fb_id = fb_user.user_fb_id

Is not a complete and accurate SQL statement to generate a result set. 不是完整而准确的SQL语句来生成结果集。 As You can see this is missing keywords 'SELECT' and 'FROM'. 如您所见,这缺少关键字“ SELECT”和“ FROM”。 I suppose you could eliminate the left join and it would work but I don't think that would meet result in the desired results. 我想您可以消除左连接,它会起作用,但我认为这样做不会达到预期的结果。

By eliminating the T4, the engine isn't attempting to parse the data between the ()'s as a valid SQL statement, instead it just see's the ()'s as order of operation and since the () don't matter on the outer portion the engine doesn't perceive a syntax error. 通过消除T4,引擎不会尝试将()之间的数据解析为有效的SQL语句,而是仅将()视为操作顺序,因为()无关紧要引擎的外部看不到语法错误。

Either have Query 1 or Query 2 Query 1Query 2

Query 1 查询1

SELECT * 
FROM 
    (
        SELECT * 
        FROM 
            (
                SELECT cu.ID as id, name, ego_id, u.fb_id 
                FROM contact_users as cu 
                JOIN users as u 
                 ON cu.ego_id = u.ID
            ) temp 
        LEFT OUTER JOIN fb_user 
            ON temp.fb_id = fb_user.user_fb_id
    ) T4

Query 2 查询2

SELECT cu.ID as id, name, ego_id, u.fb_id, fb_user.* 
FROM contact_users as cu 
JOIN users as u 
 ON cu.ego_id = u.ID 
LEFT OUTER JOIN fb_user 
 ON u.fb_id = fb_user.user_fb_id

I prefer Query 2 我更喜欢查询2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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