red8alice
red8alice

Reputation: 199

SQL Query by a foreign key which needs to be filtered by a value in different table and column

I have two tables X and Y:

I can only get the Name value from an input.

I need to get all rows in Y which match the Name as found in X

How do I write this SQL query?

I have been through a few tutorial by I am unable to understand how to achieve this. Any help would be great.

Thank you

Upvotes: 0

Views: 958

Answers (3)

Ali Baig
Ali Baig

Reputation: 3867

SELECT Y.[Id], Y.[Name]
FROM [dbo].[Y]
INNER JOIN [dbo].[X] ON [Y].ForeignKeyId = X.Id
WHERE X.Name LIKE '%YOUR_Query%'

Upvotes: 0

Ylnyorulmaz
Ylnyorulmaz

Reputation: 26

Select * 
from y 
left join x on x.id = y.xid 
where x.name = @nameparameter

A query like above should do the job. I can explain further if you will

Upvotes: 1

Stephan Lechner
Stephan Lechner

Reputation: 35154

If you just need the attributes of 'y', use

select y.* from y join x on y.x = x.id where x.name = 'your desired name'.

Upvotes: 1

Related Questions