首页 > 解决方案 > 两个子查询之间的 AND 操作

问题描述

这些是我的表:https ://ibb.co/mqX6wy

我试图找到所有在银行有贷款和账户的客户。

我正在使用 MYSQL(PHP myadmin 客户端)

查询应返回Hayes,Jones,Smith

Select customer_name 
  from (
    SELECT borrower.customer_name 
      FROM borrower, loan 
      WHERE borrower.loan_number = loan.loan_number) 
        AND (SELECT depositor.customer_name 
               FROM depositor,account 
               WHERE depositor.account_number = account.account_number)

执行上述查询会抛出:

Error: *#1064 - You have an error in your SQL syntax; check the manual 
that corresponds to your MariaDB server version for the right syntax to use
near 'AND  (SELECT depositor.customer_name FROM depositor,account WHERE 
depositor.acco' at line 1*

标签: sqldatabase

解决方案


根据您的表结构,我认为您需要以下查询

    Select t1.customer_name from 
    (
     SELECT b.customer_name FROM borrower b inner join
     on loan l b.loan_number = l.loan_number) as t1 
     inner join

    (
   SELECT depositor.customer_name FROM depositor d inner join account a on 
   d.account_number = a.account_number
    ) t2
    on t1.customer_name=t2.customer_name 

推荐阅读