Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 .NET Inside SQL Server (2005)
 Select Data based on Another Table

Author  Topic 

nadhym
Starting Member

2 Posts

Posted - 2012-04-10 : 04:35:04
Hi..
I need a query to select data from two tables and set the value of a column based on the data on table 2.
example:
Table 1 contain student name and ID.
Table 2 contain ID and grade.

result should contain, the ID, name ( selected from table 1 based on the ID) and grade.

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-10 : 11:03:28
select
t1.ID,
t1.name,
t2.grade
from table1 as t1
left join table2 as t2 on t2.id = t1.id


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

nadhym
Starting Member

2 Posts

Posted - 2012-04-11 : 01:35:09
Thank You Webfred.. :)
Example was just a Sample
But i want the name to be selected based on the ID.
Which means if the ID is '70', the query should search it from Table 1.

Thank you Again..
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-04-11 : 02:57:11
Do you know the WHERE clause?

Just add
WHERE t1.ID = 70
at the end of the query


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -