加入时MySQL语法错误

时间:2014-10-20 07:41:31

标签: mysql sql

我目前正在教自己使用MySQL。问题是,自我教学并不总是那么容易,而且我常常陷入困境。通常情况下,我会试着让自己失望,但今天不是这样的一天。

我正在使用SQLFiddle(http://sqlfiddle.com/)对数据库运行查询,直到我找到一些更永久的软件解决方案。

我的问题出在我的查询中。

(SELECT customer.customername, the_account.balance, branch.branch_name    
   FROM customer, has_account, the_account, branch   
  WHERE customer.ssn = has_account.assn
    AND has_account.ano = the_account.accountno
    AND the_account.branchid = branch.branchid) AS a
JOIN
(SELECT customer.customername, loan.amount, branch.branch_name 
   FROM customer, has_loan, loan, branch
  WHERE customer.ssn = has_loan.lssn
    AND has_loan.lno = loan.loanno
    AND loan.branchid = branch.branchid) AS b
ON has_account.assn = has_loan.lssn;

现在,我确信语法错误对那些熟悉SQL的人来说是显而易见的,但我只是陷入了困境,似乎无法自拔。

我的理解是,我至少可以像这样尝试加入任意两个表。

有关我的数据库,查询等的完整概述,请转到http://sqlfiddle.com/#!2/1943b/85

请,温柔。我知道这可能是一个新手的错误,但是你自己学习这些东西并不像你想象的那么容易。

UPDATE1:

所以在尝试了建议之后:

(SELECT has_account.assn as assn, customer.customername, the_account.balance, branch.branch_name    
   FROM customer, has_account, the_account, branch   
  WHERE customer.ssn = has_account.assn
    AND has_account.ano = the_account.accountno
    AND the_account.branchid = branch.branchid) AS a

JOIN

(SELECT has_loan.lssn as lssn, customer.customername, loan.amount, branch.branch_name 
   FROM customer, has_loan, loan, branch
  WHERE customer.ssn = has_loan.lssn
    AND has_loan.lno = loan.loanno
    AND loan.branchid = branch.branchid) AS b

ON b.lssn = a.assn;

看起来很好,并且对我来说世界上有意义,SQLFiddle抱怨道:

  

您的SQL语法有错误;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   'AS a JOIN附近(SELECT has_loan.lssn as lssn,customer.customername,   loan.amount,bra'在第5行:( SELECT has_account.assn as assn,   customer.customername,the_account.balance,branch.branch_name FROM   customer,has_account,the_account,branch WHERE customer.ssn =   has_account.assn AND has_account.ano = the_account.accountno AND   the_account.branchid = branch.branchid)作为一个加入(选择   has_loan.lssn as lssn,customer.customername,loan.amount,   branch.branch_name FROM customer,has_loan,loan,branch WHERE   customer.ssn = has_loan.lssn AND has_loan.lno = loan.loanno AND   loan.branchid = branch.branchid)AS b ON b.lssn = a.assn

(以防万一有人想要完整的错误报告!)

1 个答案:

答案 0 :(得分:3)

select * from
(SELECT has_account.assn as assn, customer.customername, the_account.balance, branch.branch_name    
   FROM customer, has_account, the_account, branch   
  WHERE customer.ssn = has_account.assn
    AND has_account.ano = the_account.accountno
    AND the_account.branchid = branch.branchid) AS a
JOIN
(SELECT has_loan.lssn as lssn, customer.customername, loan.amount, branch.branch_name 
   FROM customer, has_loan, loan, branch
  WHERE customer.ssn = has_loan.lssn
    AND has_loan.lno = loan.loanno
    AND loan.branchid = branch.branchid) AS b
ON b.lssn = a.assn;

我个人不喜欢这样的连接,你可以用简单的方式尝试(我不确定连接条件是否正确,请检查):

SELECT
  customer.customername,
  the_account.balance,
  branch.branch_name,
  loan.amount
FROM customer
JOIN has_account on has_account.assn = customer.ssn
JOIN the_account on the_account.accountno = has_account.ano
JOIN branch on branch.branchid = the_account.branchid
JOIN has_loan on has_loan.lssn = customer.ssn
JOIN loan on loan.loanno = has_loan.lno;