从两个关联表中选择SQL

3

我在MySQL中有两个表,customercontacts

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

如果我搜索Andy,它应该返回公司A

如果我搜索John,它应该返回公司A公司B


你的问题不够清晰。如果你只想要来自客户表的结果,那么你为什么要搜索两个表呢?使用“SELECT * FROM CUSTOMER;”就可以做到这一点,而无需搜索两个表。 - kojow7
您想按照哪个标准筛选结果? - TimoStaudinger
2
可能是重复的问题:如何在MySQL中连接两个表? - Andy Hoffner
看一下我的更新——希望能解释得更清楚。 - charlie
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接