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
 help needed in join query

Author  Topic 

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2008-12-30 : 06:48:14
the folowing are my tables:

tblImage: table values:

imageid - bigint 1 jesus.jpg
imageurl- varchar(max) 2 sachin.jpg

tblUserDetail:

userid- bigint 1 sql 1,2
username - varchar(50)
imageid-text

so my out put should be
name imageurl
sql jesus.jpg
sql sachin.jpg

please give me some sample query to achieve this please

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-30 : 06:59:17
SELECT t2.username,t1.imageurl
FROM tblimage t1
INNER JOIN tblUserDetail t2 ON t1.imageid LIKE t2.imageid

Jai Krishna
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2008-12-30 : 07:05:50
quote:
Originally posted by raky

select u.username as name,i.imageurl
from tblUserDetail u
inner join tblImage i on u.imageurl = i.imageurl and i.imageid = u.imageid



I think imageid in tblImage as text datatype so please check it once...
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-30 : 07:11:49
select u.username,i.imageurl from tblImage i inner join tbluserdetail u on
('%,' + stuff((select ','+ cast(imageid as varchar(32)) from tblimage for xml path ('')),1,1,'') + ',%') like (select ('%,' + cast(imageid as varchar(32)) + ',%') from tbluserdetail)


try this
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2008-12-30 : 07:16:34
if my table structure is wrong please give me sample table structure for my need please
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-30 : 07:21:23
hi, sqlover
no ur getting the required output once check the above query
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 11:26:04
have you heard of normalization? why are you storing values as comma separated in imageid field?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-30 : 11:55:17
[code]
SELECT m.username,i.imageurl
FROM
(
SELECT t.userid,t.username,f.Val
FROM tblUserDetail t
CROSS APPLY dbo.ParseValues(t.imageid,',') f
)m
INNER JOIN tblImage i
ON m.Val=i.imageid
[/code]

parsevalues can be found here

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=113563
Go to Top of Page

sqllover
Constraint Violating Yak Guru

338 Posts

Posted - 2008-12-31 : 00:44:13
thank you friends for the efficient answers
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-31 : 00:55:12
you're welcome
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-31 : 01:08:32
Welcome

Jai Krishna
Go to Top of Page
   

- Advertisement -