Sara
Sara

Reputation: 595

Selecting data from two tables with sql query

I have 2 tables

Course ( ID Date Description Duration Meatier_ID Promotion_ID).

second table

Ensign( E_Id Meatier_Id Promotion_Id)

infarct i have an id based on that id i have to Select data from Ensign where id=Eng_Id then i need to select data from Course where Meatier_Id and Promotion_Id in table Course are equal to Meatier_Id and Promotion_Id to data selected in earlier query

can i do it using one S q l query thanks

Br Sara

Upvotes: 1

Views: 4007

Answers (3)

NigelK
NigelK

Reputation: 8480

Join the two tables together on Meater_ID and Promotion_ID. Then select those rows where Eng_Id is the id you are working with.

SELECT *
FROM Course c
INNER JOIN Ensign e
   ON e.Meatier_ID = c.Meatier_ID
  AND e.Promotion_ID = c.Promotion_ID
WHERE e.Eng_Id = <id value here>

EDIT:

The above should work for SQL Server. For derby, try:

SELECT *
FROM Course
INNER JOIN Ensign 
  ON Ensign.Meatier_ID = Course.Meatier_ID
 AND Ensign.Promotion_ID = Course.Promotion_ID
WHERE Ensign.Eng_Id = <id value here>

Upvotes: 0

user3036372
user3036372

Reputation: 24

select e.E_Id, e.Meatier_Id, e.Promotion_Id, c.ID, c.Date, c.Description, c.Duration from Ensign as e inner join course as c where e.Meatier_Id=c.Meatier_Id and e.Promotion_Id=c.Promotion_Id and e.E_Id=@Eng_Id

Upvotes: 0

Pierre
Pierre

Reputation: 9052

Your question is bit vague, But I gave it a try

--These two variables take the place for your 'Earlier Query' values
DECLARE @Meatier_ID INT = 100,
        @Promotion_Id INT = 15

--The query
SELECT *
FROM Course AS C
INNER JOIN Ensign AS E ON C.ID = E.E_Id
WHERE C.Meatier_ID = @Meatier_ID
AND C.Promotion_Id = @Promotion_Id

Upvotes: 1

Related Questions