如何将查询结果联接到另一个查询

时间:2019-02-28 19:11:39

标签: mysql sql database

SELECT customer_trn, avg(balance) FROM 
  ( 
  SELECT depositor.customer_trn, account.account_number, account.balance FROM 
    account 
      INNER JOIN savings_account ON account.account_number = savings_account.account_number 
      INNER JOIN depositor ON depositor.account_number = account.account_number 
    UNION ALL 
  SELECT depositor.customer_trn, account.account_number, account.balance FROM 
    account 
      INNER JOIN checking_account ON account.account_number = checking_account.account_number 
      INNER JOIN depositor ON depositor.account_number = account.account_number 
  ) 
  AS subquery2 GROUP BY customer_trn ;

上面的查询产生以下结果。

+--------------+---------------+
| customer_trn | avg(balance)  |
+--------------+---------------+
| 125-233-001  | 252500.000000 |
| 125-233-002  |   3732.500000 |
| 125-233-004  | 480002.000000 |
| 125-233-005  |  17000.000000 |
| 125-233-006  |  17000.000000 |
| 125-233-007  |   5000.000000 |
| 125-233-008  |   5000.000000 |
+--------------+---------------+

我正在尝试加入该查询以获取名称(从下表中)以及初始结果。本质上,我想将customer_trn(主键)链接为公共列,以便使表结果更有意义。 它将包含客户名称,客户trn和平均余额。

CREATE TABLE customer
   (
    customer_trn   varchar(50) NOT NULL UNIQUE,
    customer_name   varchar(50) NOT NULL,
    customer_street  varchar(50)  NOT NULL,
    customer_city  varchar(50)  NOT NULL,
    PRIMARY KEY(customer_name)
    );

因此它看起来像:

+--------------+---------------+---------------+
| customer_trn | avg(balance)  |customer_name  |
+--------------+---------------+---------------+
| 125-233-001  | 252500.000000 |John           |
| 125-233-002  |   3732.500000 |Bobby          |  
| 125-233-004  | 480002.000000 |James          |
| 125-233-005  |  17000.000000 |King           |
| 125-233-006  |  17000.000000 |Raven          |
| 125-233-007  |   5000.000000 |Mark           |
| 125-233-008  |   5000.000000 |Sam            |
+--------------+---------------+---------------+ 

2 个答案:

答案 0 :(得分:1)

与客户表和子查询一起使用联接

SELECT subquery2.customer_trn, c.customer_name   ,avg(balance) FROM 
  ( 
  SELECT depositor.customer_trn, account.account_number, account.balance FROM 
    account 
      INNER JOIN savings_account ON account.account_number = savings_account.account_number 
      INNER JOIN depositor ON depositor.account_number = account.account_number 

    UNION ALL 
  SELECT depositor.customer_trn, account.account_number, account.balance FROM 
    account 
      INNER JOIN checking_account ON account.account_number = checking_account.account_number 
      INNER JOIN depositor ON depositor.account_number = account.account_number 
  ) 
  AS subquery2 join customer c on c.customer_trn =subquery2.customer_trn 

GROUP BY subquery2.customer_trn,c.customer_name ;

答案 1 :(得分:1)

假设您有一个表customer,该表的customer_name和id用于加入customer_trn 您可以

SELECT t1.customer_trn, t1.cust_avg, c.username 
from (
SELECT customer_trn, avg(balance)  cust_avg
FROM 
  ( 
  SELECT depositor.customer_trn, account.account_number, account.balance 
  FROM account 
      INNER JOIN savings_account ON account.account_number = savings_account.account_number 
      INNER JOIN depositor ON depositor.account_number = account.account_number 
    UNION ALL 
   SELECT depositor.customer_trn, account.account_number, account.balance FROM 
    account 
      INNER JOIN checking_account ON account.account_number = checking_account.account_number 
      INNER JOIN depositor ON depositor.account_number = account.account_number 
  )   AS subquery2 
  GROUP BY customer_trn 

) t1 
inner join  customer ON c.id = t1.customer_trn