2015-06-01 69 views
3

我在MySQL中,customer两个表,并contactsSQL从两个链接表

customer每行可以有多个排在contacts,并选择contacts.company将等于customer.sequence

如何运行一个查询来搜索这两个表并仅返回customer表中的结果?

例如,如果我有这样的数据:

customer: 

sequence = 123 
company = Company A 

sequence = 321 
company = Company B 

contacts: 

company = 123 
forename = John 

company = 123 
forename = Steve 

company = 321 
forename = Joe 

company = 321 
forename = Andy 

company = 321 
forename = John 

,我搜索安迪 - 它应该返回Company A

如果我要寻找的约翰,它应该返回Company ACompany B

+0

你的问题不够清楚。如果您只需要客户表的结果,那么您在两个表中搜索什么? SELECT * FROM CUSTOMER;将会这样做而不搜索两个表。 – kojow7

+0

你想过滤你的结果? – Timo

+2

可能重复[如何连接两个表mysql?](http://stackoverflow.com/questions/3536283/how-to-join-two-tables-mysql) – ahoffner

回答

0
select 
    C.Company 
from 
    Customer, 
    Contacts 
where 
    Customer.Sequence = Contacts.Company 
    and Contacts.Forename = '<your search name goes here>'