Reputation: 199
I have two tables X and Y:
X
has columns ID
(primary key), Name
Y
has a foreign key to X
referencing the ID
columnI 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
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
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
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