【问题标题】:SQL INNER JOIN exceptionSQL INNER JOIN 异常
【发布时间】:2015-03-17 11:05:01
【问题描述】:

我是 SQL 查询的新手,我正在尝试连接两个表

我需要获取userID = 2的关注者的所有数据

这是我得到的错误:Syntax error: Encountered "INNER" at line 1, column 39.

这是我运行的 SQL 查询:

SELECT * FROM FOLLOWER 
WHERE userID = "2" 
INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID 
ORDER BY USERS.follower_count ASC

我的数据库中的表是:

关注者


  • 身份证
  • 用户ID
  • Follower_userID

用户


  • 用户ID
  • 用户名
  • 密码
  • 昵称

附言 我正在使用 Apache Derby。

非常感谢你们。

【问题讨论】:

  • 附注:不要在查询中对字符串文字 "2" 使用双引号 (")。通过转动[QUOTED_IDENTIFIER](http://msdn.microsoft.com/en-us/library/ms174393.aspx),它具有不同的含义(标识符引用者:))。此外,您不必将整数值作为字符串文字传递:它可能会导致意外结果或不必要的隐式数据类型转换。

标签: sql exception syntax-error derby


【解决方案1】:

where 子句的位置不正确

SELECT 查询的结构是

SELECT fields
FROM tables
WHERE conditions
ORDER BY fields

所以你的查询应该是

SELECT * 
FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID
WHERE userID="2" 
ORDER BY USERS.follower_count ASC

【讨论】:

    【解决方案2】:

    试试这个说法:

    SELECT * FROM FOLLOWER Fl
    WHERE userID="2" 
    INNER JOIN USERS Us ON Us.userID = Fl.Follower_userID
    ORDER BY USERS.follower_count ASC
    

    让我知道它是否有效

    【讨论】:

      【解决方案3】:

      先用join,再用where。

      SELECT * FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID WHERE userID=2  ORDER BY USERS.follower_count ASC
      

      【讨论】:

        【解决方案4】:

        试试这个 SELECT * FROM FOLLOWER INNER JOIN USERS ON FOLLOWER.Follower_userID = USERS.userID WHERE FOLLOWER.userID="2" ORDER BY USERS.follower_count ASC

        希望对你有帮助

        【讨论】:

          【解决方案5】:

          Inner Join 语法

          SELECT *
          FROM Table1 AS T1
              INNER JOIN Table2 AS T2
              ON T1.Table1ColName = T2.Table2ColName
          ORDER BY T1.Table1ColName
          

          请参阅此链接以获取Inner Join

          像这样改变你的查询

          SELECT * FROM FOLLOWER
          INNER JOIN USERS 
                     ON FOLLOWER.Follower_userID = USERS.userID
          WHERE userID="2"  
          ORDER BY USERS.follower_count ASC
          

          【讨论】:

            【解决方案6】:

            where with join的规则是先全部join再给出过滤更多数据的where条件..

            所以你的 where 条件就像上面所有建议的那样放在内部连接之后。

            select * 
            from yourtable
            join yourothertable
            where condition if you want
            

            http://bytes.com/topic/sql-server/answers/850159-performance-conditions-where-clause-vs-conditions-inner-join

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2018-03-09
              • 2013-07-01
              • 1970-01-01
              • 2020-08-23
              • 2011-03-25
              • 2012-06-01
              • 1970-01-01
              相关资源
              最近更新 更多