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
 General SQL Server Forums
 New to SQL Server Programming
 SELECT FROM TWO TABLE FOR ONE RECORD

Author  Topic 

oguzkaygun
Yak Posting Veteran

53 Posts

Posted - 2011-10-08 : 10:53:31
Hello
I have two tables like below.
Table 1
ID NAME TABLE2ID
1 Apple 1

Table 2
ID NAME NAMEID
1 Yellow 1
1 Yellow 2

I want Grid display like below
Apple Yellow. Only one record.
if there second apple as name at Table 1 like below
ID NAME TABLE2ID
1 Apple 1
2 Apple 1
Grid should be display like below.
Apple Yellow
Apple Yellow.
How can i do this ?
Thank you.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-08 : 11:10:38
[code]
SELECT t1.NAME,t2.NAME
FROM Table1 t1
JOIN Table2 t2
ON t2.ID = t1.TABLE2ID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

oguzkaygun
Yak Posting Veteran

53 Posts

Posted - 2011-10-08 : 11:16:20
Hello
I did like below
Select name, table2id ( select top 1 columname From table2 t2 Where t2.id =t1.table2id ) from table1 t1
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-08 : 11:18:45
quote:
Originally posted by oguzkaygun

Hello
I did like below
Select name, table2id ( select top 1 columname From table2 t2 Where t2.id =t1.table2id ) from table1 t1


ok you want only one record to be displayed of duplicates? then isnt distinct enough?

SELECT DISTINCT t1.NAME,t2.NAME
FROM Table1 t1
JOIN Table2 t2
ON t2.ID = t1.TABLE2ID


use of subqueries can hurt performance when you're dealing with large data

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

oguzkaygun
Yak Posting Veteran

53 Posts

Posted - 2011-10-08 : 11:26:56
Hello
Yes. one record for dublicate records.
But if they are in table2.
if there are two apple as name at table1, grid should be display two records.
like below
apple yellow
apple yellow.

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-08 : 11:30:41
quote:
Originally posted by oguzkaygun

Hello
Yes. one record for dublicate records.
But if they are in table2.
if there are two apple as name at table1, grid should be display two records.
like below
apple yellow
apple yellow.




distinct will return only one set of values from record.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

oguzkaygun
Yak Posting Veteran

53 Posts

Posted - 2011-10-08 : 11:34:45
ok. i see now. thank you for help.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-08 : 12:34:34
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

jassi.singh
Posting Yak Master

122 Posts

Posted - 2011-10-10 : 02:28:22
Hi,

Try following
select table1.Id, table1.Name,Table2.name from table1 inner join table2 on table1.Table2ID = table2.Id


Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-10 : 02:31:51
quote:
Originally posted by jassi.singh

Hi,

Try following
select table1.Id, table1.Name,Table2.name from table1 inner join table2 on table1.Table2ID = table2.Id


Please mark answer as accepted if it helped you.

Thanks,
Jassi Singh


Can you AT LEAST try this for posted sample data and see what you got before suggesting anything?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -