gpt4 book ai didi

mysql - SQL 从两个链接表中选择

转载 作者:行者123 更新时间:2023-11-29 01:02:48 25 4
gpt4 key购买 nike

我在 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 - 它应该返回 Company A

如果我搜索 John,它应该返回 Company ACompany B

最佳答案

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

关于mysql - SQL 从两个链接表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30584326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com