SQL INNER JOIN 异常

SQL INNER JOIN exception

我是 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

我的数据库中的表是:

追随者


用户


P.S 我正在使用 Apache Derby。

非常感谢你们。

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

试试这个语句:

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

让我知道它是否有效

先用join再用where。

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

Inner Join 语法

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

Inner Join

引用此 link

像这样更改您的查询

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

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

希望对您有所帮助

where with join的规则是先让所有join然后给出where条件过滤更多的数据..

所以你的 where 条件就像上面所有建议的那样放在 inner join 之后。

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