【问题标题】:Unable to join 3 tables properly无法正确加入 3 个表
【发布时间】:2015-06-21 08:44:23
【问题描述】:

在了解自然联接的同时,我遇到了以下查询:

查找在银行开户并居住在哈里森的客户所在的分行名称

书中的关系代数表达式如下:

用查询实现同样的功能:

select distinct a.branch_name from depositor d, account a, customer where d.account_number=a.account_number and customer.customer_city='Harrison'; 

我得到如下的虚假元组:

+-------------+
| branch_name |
+-------------+
| Perryridge  |
| Downtown    |
| Brighton    |
| Redwood     |
| Mianus      |
| Round Hill  |
+-------------+
6 rows in set (0.00 sec)

但查询必须仅返回 Brighton 和 Perryridge 基于以下架构:

mysql> select * from account;
+----------------+-------------+---------+
| account_number | branch_name | balance |
+----------------+-------------+---------+
| A101           | Downtown    |     500 |
| A102           | Perryridge  |     400 |
| A201           | Brighton    |     900 |
| A215           | Mianus      |     700 |
| A217           | Brighton    |     750 |
| A222           | Redwood     |     700 |
| A305           | Round Hill  |     350 |
+----------------+-------------+---------+
7 rows in set (0.00 sec)

mysql> select * from customer;
+---------------+-----------------+---------------+
| customer_name | customer_street | customer_city |
+---------------+-----------------+---------------+
| Adams         | Spring          | Pittsfield    |
| Brooks        | Senator         | Brooklyn      |
| Curry         | North           | Rye           |
| Glenn         | Sand Hill       | Woodside      |
| Green         | Walnut          | Stamford      |
| Hayes         | Main            | Harrison      |
| Johnson       | Alma            | Palo Alto     |
| Jones         | Main            | Harrison      |
| Lindsay       | Park            | Pittsfield    |
| Smith         | North           | Rye           |
| Turner        | Putnam          | Stamford      |
| Williams      | Nassau          | Princeton     |
+---------------+-----------------+---------------+
12 rows in set (0.00 sec)

mysql> select * from depositor;
+---------------+----------------+
| customer_name | account_number |
+---------------+----------------+
| Hayes         | A102           |
| Johnson       | A101           |
| Johnson       | A201           |
| Jones         | A217           |
| Lindsay       | A222           |
| Smith         | A215           |
| Turner        | A305           |
+---------------+----------------+
7 rows in set (0.00 sec)

我在哪里犯错了?

【问题讨论】:

  • 我看到你没有在 Where 子句中加入 customer
  • 但是我有 from 子句@The 中提到的所有 3 个表

标签: mysql relational-algebra natural-join


【解决方案1】:

您可能会忘记存款人和客户之间的联系。

depositor.customer_name = customer.customer_name

所以整个查询应该是:

SELECT DISTINCT a.branch_name  
FROM depositor d, account a, customer  
WHERE d.account_number = a.account_number  
AND d.customer_name = customer.customer_name  
AND customer.customer_city='Harrison'

结果:

+-------------+
| branch_name |
+-------------+
| Perryridge  |
| Brighton    |
+-------------+
2 rows in set (0.00 sec)

【讨论】:

    【解决方案2】:

    你没有为customer表做join,你的查询应该是这样的

    Select a.branch_name 
    From depositor d 
      Join account a
        on d.account_number=a.account_number 
      Join customer as c
        on d.customer_name  = c.customer_name 
    Where c.customer_city='Harrison'
    

    我不知道如何将 customer 表加入到 depositor 中,可能是按名称,或者如果您有一些密钥,只需替换它即可获得结果。

    如何在 where 子句 useful link 中进行连接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-19
      • 2016-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多