Adam Baranyai
Adam Baranyai

Reputation: 3867

SQL: How to select by linked table column where there are multiple rows in linked table?

Let's presume I have the following two tables:

Contacts

id (INT PRIMARY)

name (VARCHAR)



Emails

email_Id (INT PRIMARY)
email_address (VARCHAR)
contacts_id (INT > the link to the Contacts table)

These two tables are linked by contacts_id, so basically each contact may have multiple e-mail addresses associated to it. How could I make an SQL query, which finds EVERY contact, whose email address (email_address) field is LIKE query?

Upvotes: 0

Views: 37

Answers (2)

Aabid
Aabid

Reputation: 941

You can use left joins it will give all results

select contacts.* from contacts left join Emails on(contacts.id=Emails.contacts_id) where Emails.email_address LIKE '%text%'

ignore syntax error if any.

Upvotes: 1

ricky89
ricky89

Reputation: 1396

Try this:

SELECT DISTINCT ContactId FROM Contacts AS C
INNER JOIN Email AS E ON E.contactID = C.ContactID
WHERE E.Email LIKE '%Text%'

Upvotes: 1

Related Questions