Mandeep Singh
Mandeep Singh

Reputation: 37

Get data from two tables in SQL Server, on where condition of one column

I have two tables Exercises & Workouts. I want to fetch data from these two tables as follows:

Table structure (screenshot): table above is workout table & below that is exercises table.

enter image description here

I want result will be like (screenshot):

enter image description here

How can I get the above result from these two tables on where condition of workout_name ='Testing' ?

Upvotes: 0

Views: 1700

Answers (2)

Yogesh Sharma
Yogesh Sharma

Reputation: 50163

Simply join them

select w.workout_name, e.exercise_name, e.exercise_image 
from Workouts w
join Exercises e on e.exercise_name = w.exercise_name
where w.workout_name = 'Testing'

Upvotes: 4

Pawan Kumar
Pawan Kumar

Reputation: 2011

We can also use cross apply for this-

SELECT w.workout_name, w.exercise_name, x.exercise_image 
FROM Workouts w
CROSS APPLY 
( 
    SELECT e.exercise_image FROM Exercises e WHERE e.exercise_name = w.exercise_name 
) x
WHERE w.workout_name = 'Testing'

Upvotes: -1

Related Questions