连接3个三个表时使用内部连接查询时出错

时间:2017-07-05 05:56:47

标签: mysql sql-server

我在使用连接查询时尝试连接三个表但是在执行代码时我在FROM子句中遇到语法错误。我已经编写了代码片段,请建议我的代码中出现错误。

 SELECT u.UserID, t.UserName,u.email, d.debit,d.credit,d.hand_on_cash
  FROM userdetails u JOIN usertable t  JOIN userdata d
  WHERE u.UserID=t.UserID
   AND u.UserID=d.UserID
  ORDER BY u.UserID,t.UserID

2 个答案:

答案 0 :(得分:1)

您正在混合ON并且不需要使用WHERE

SELECT u.UserID, t.UserName,u.email, d.debit,d.credit,d.hand_on_cash
      FROM userdetails u JOIN usertable t ON u.UserID=t.UserID
      JOIN userdata d ON u.UserID=d.UserID
       --WHERE u.UserID=t.UserID
       --AND u.UserID=d.UserID
      ORDER BY u.UserID,t.UserID

答案 1 :(得分:1)

您正在混合使用旧样式和新样式连接语法。你应该使用后者:

SELECT
    u.UserID,
    t.UserName,
    u.email,
    d.debit,
    d.credit,
    d.hand_on_cash
FROM userdetails u
INNER JOIN usertable t
    ON u.UserID = t.UserID
INNER JOIN userdata d
    ON u.UserID = d.UserID
ORDER BY
    u.UserID,
    t.UserID

如果您真的想使用旧式的Oracle样式连接,它将如下所示:

SELECT
    u.UserID,
    t.UserName,
    u.email,
    d.debit,
    d.credit,
    d.hand_on_cash
FROM userdetails u, usertable t, userdata d   -- commas in the FROM clause
WHERE                                         -- not nice, but it should run
    u.UserID = t.UserID AND
    u.UserID = d.UserID                       -- the ON condition appears here
ORDER BY
    u.UserID,
    t.UserID