Selecting SQL from two related tables

I have two tables in MySQL customer

andcontacts

each line in customer

can have multiple lines in contacts

and contacts.company

will equalcustomer.sequence

how can I run a query to search both tables and only return results from the table customer

?

for example, if I have this data:

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

      

and I'm looking for Andy - he must return Company A

If I am looking for John, he must return Company A

andCompany B

+3


source to share


1 answer


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

      



0


source







All Articles