在select语句中获取重复值

时间:2017-12-21 20:34:08

标签: mysql sql

我创建了两个表customer和customer_address_details,如下所示。我有customer_id作为来自customer表的customer_address的外键。

Customer Table:
+-------------+---------------+--------------+
| CUSTOMER_ID | CUSTOMER_NAME | CUSTOMER_DOB |
+-------------+---------------+--------------+
|           1 | PHILLIP       | 1983-01-05   |
|           2 | ROBERT        | 1981-01-05   |
+-------------+---------------+--------------+

Customer_Address Table:
+------------+-------------------------+--------------+---------------+-----------------+-------------+
| ADDRESS_ID | ADDRESS_STREET          | ADDRESS_CITY | ADDRESS_STATE | ADDRESS_ZIPCODE | CUSTOMER_ID |
+------------+-------------------------+--------------+---------------+-----------------+-------------+
|          1 | 12345 mlk pkwy | san Antonio       | TEXAS         |           75025 |           1 |
|          2 | 12345 main st           | Austin        | TEXAS         |           75035 |           2 |
+------------+-------------------------+--------------+---------------+-----------------+-------------+

当我提供以下查询时,它会显示重复的行。

从客户a,customer_address b中选择a.customer_name,b.address_street,其中a.customer_id = 1;

+---------------+-------------------------+
| customer_name | address_street          |
+---------------+-------------------------+
| PHILLIP       | 12345 mlk pkwy |
| PHILLIP       | 12345 main st          |
+---------------+-------------------------+

即使我要求为customer_id = 1选择地址,你能否告诉我为什么它会提供重复的行?

这是我的表格描述

+---------------+-------------+------+-----+---------+-------+
| Field         | Type        | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| CUSTOMER_ID   | int(11)     | NO   | PRI | NULL    |       |
| CUSTOMER_NAME | varchar(50) | YES  |     | NULL    |       |
| CUSTOMER_DOB  | date        | YES  |     | NULL    |       |
+---------------+-------------+------+-----+---------+-------+

+-----------------+-------------+------+-----+---------+-------+
| Field           | Type        | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| ADDRESS_ID      | int(11)     | NO   |     | NULL    |       |
| ADDRESS_STREET  | varchar(50) | YES  |     | NULL    |       |
| ADDRESS_CITY    | varchar(30) | YES  |     | NULL    |       |
| ADDRESS_STATE   | varchar(30) | YES  |     | NULL    |       |
| ADDRESS_ZIPCODE | int(11)     | YES  |     | NULL    |       |
| CUSTOMER_ID     | int(11)     | NO   | MUL | NULL    |       |
+-----------------+-------------+------+-----+---------+-------+

4 个答案:

答案 0 :(得分:4)

您需要一个条件来加入您的表格,因此您没有与每个客户匹配的每个地址。​​

这样的东西
select a.customer_name,b.address_street 
from customer a, customer_address b 
where a.customer_id=1 and a.customer_id=b.customer_id

select a.customer_name,b.address_street 
from customer a inner join customer_address b 
 on a.customer_id=b.customer_id
where a.customer_id=1

答案 1 :(得分:1)

您应该加入表格:

select a.customer_name, b.address_street from customer a
inner join customer_address b 
on a.customer_id = b.customer_id
where a.customer_id = 1;

这表示您希望聚合表并从这个新关系中返回行。

来源:https://dev.mysql.com/doc/refman/5.7/en/join.html

答案 2 :(得分:0)

为了获得最佳实践,请使用此查询: (适合时间复杂度)

select a.customer_name,b.address_street from customer a inner join customer_address b on a.customer_id=b.customer_id where a.customer_id=1

你也可以尝试这样的事情: (当涉及时间复杂性时,不推荐作为最佳实践)

select a.customer_name,b.address_street from customer a, customer_address b where a.customer_id=1 and a.customer_id=b.customer_id

答案 3 :(得分:0)

您需要加入表格。试试这个:

Select C.Customer_name, B.ADDRESS_STREET
 from Customer C
 join Customer_Address B on C.customer_id = B.Customer_id
 where C.Customer_id = 1