Author |
Topic |
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-02-05 : 08:44:14
|
I have the following two tables:Table 1: #LoID LName--- ---------789 L1963 L2Table 2: #PDID PName---- -----------789 P1789 P2874 P3I want to select LName and PName from two tables.-->where ID = '789' Result:ID Name----- ---------789 L1789 P1789 P2Thanks |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-05 : 09:03:34
|
One way to do would be to use a UNIONSELECT ID, PName AS NAME FROM #PDWHERE ID = '789'UNIONSELECT a.ID, a.LNameFROM #Lo a INNER JOIN #PD b ON b.ID = a.IDWHERE b.ID ='789' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-05 : 10:07:12
|
quote: Originally posted by James K One way to do would be to use a UNIONSELECT ID, PName AS NAME FROM #PDWHERE ID = '789'UNIONSELECT a.ID, a.LNameFROM #Lo a INNER JOIN #PD b ON b.ID = a.IDWHERE b.ID ='789'
what the purpose of the join with first table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-02-05 : 10:13:28
|
quote: what the purpose of the join with first table?
No need to - left over from how I started the query. Strike that. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-05 : 10:14:39
|
quote: Originally posted by James K
quote: what the purpose of the join with first table?
No need to - left over from how I started the query. Strike that.
Ok..was a bit confused seeing that. Thought I missed something------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-02-06 : 00:13:22
|
Thanks..Its work fine. and the query working only Same GUID in the two tables.Now i need to retrieve the data according to the GUID from any one tableEaxmple:where Id= '963'Result:ID Name----- --------963 L2Thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-06 : 00:32:18
|
same query itself just use new id valueSELECT ID, PName AS NAME FROM #PDWHERE ID = '963'UNIONSELECT a.ID, a.LNameFROM #Lo aWHERE a.ID ='963' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
teamjai
Yak Posting Veteran
52 Posts |
Posted - 2013-02-06 : 01:33:49
|
Many Thanks..i have one more question..i use this same query but i removed the column "PName" and LName.SELECT ID FROM #PDWHERE ID = '789'UNIONSELECT a.ID, FROM #Lo aWHERE a.ID ='789'Result comes only one ID,ID-------789But i need to get 3 IDs,ID-------789789789Thanks. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-02-06 : 01:35:27
|
if you want to retain duplicates use UNION ALLSELECTID FROM #PDWHEREID = '789'UNION ALLSELECTa.IDFROM#Lo aWHEREa.ID ='789' ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
|