无法正确连接 3 个表
Unable to join 3 tables properly
在了解自然联接时,我遇到了查询:
Find the names of branches with customers who have an account in the bank and live in Harrison
书上的关系代数表达式如下:
对查询执行相同的操作:
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)
我哪里出错了?
您没有加入 客户 table,您的查询应该是这样的
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 table 到 depositor 也许通过名字或者如果你有一些钥匙只是更换它,你会得到你的结果。
如何在 where 子句中进行连接 useful link
你可能忘记了存款人和客户之间的link。
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)
在了解自然联接时,我遇到了查询:
Find the names of branches with customers who have an account in the bank and live in Harrison
书上的关系代数表达式如下:
对查询执行相同的操作:
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)
我哪里出错了?
您没有加入 客户 table,您的查询应该是这样的
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 table 到 depositor 也许通过名字或者如果你有一些钥匙只是更换它,你会得到你的结果。
如何在 where 子句中进行连接 useful link
你可能忘记了存款人和客户之间的link。
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)