| Author |
Topic |
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-05 : 02:22:43
|
| Hi FriendsI have two tables named User and EventsUser Tables has FieldsUserId Interests1 2,3,4,102 1,5,6,93 0,11,12,6,9,8Events Table has FieldsEventId Interests1 0,8,6,72 1,9,12,15,103 8,9,6,5,2In user table userid 1 has interests in the following numbers2,3,4,10 likewise for userid 2 and userid 3In events table eventid 1 is shown to the users who are all having interests in any one of the numbers 0,8,6,7Samething for eventid 2 and 3so when user 1 logs in i need to select the eventids 2,3Here we need to take the interests of userid 1 (i,e) 2,3,4,10 and we need to compare the each number in event table of interestsso 2 is in eventid 3,3 and 4 is not present in event table ,10 is in eventid 2 ,so i need event id 2 and 3 row as result.Expected OutPutEventId Interests2 1,9,12,15,103 8,9,6,5,2Please help me to get the resultThanks in Advance |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-05 : 06:00:22
|
| 1 Read about normalization2 select e.* from users as u inner join events as e on ','+u.interests+',' like '%,'+cast(eventid as varchar(10))+',%'MadhivananFailing to plan is Planning to fail |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-05 : 06:33:49
|
quote: Originally posted by madhivanan 1 Read about normalization2 select e.* from users as u inner join events as e on ','+u.interests+',' like '%,'+cast(eventid as varchar(10))+',%'MadhivananFailing to plan is Planning to fail
Hi MadhiThanks for your replyI used your query but i can't able to get the correct answerPlease help me to get the correct result.Your help will be much appreciated.Thanks |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-05 : 08:25:15
|
| select e.* from users as u inner join events as e on ','+u.interests+',' like '%,'+cast(eventid as varchar(10))+',%' where u.userid=1MadhivananFailing to plan is Planning to fail |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-05 : 08:49:32
|
quote: Originally posted by madhivanan select e.* from users as u inner join events as e on ','+u.interests+',' like '%,'+cast(eventid as varchar(10))+',%' where u.userid=1MadhivananFailing to plan is Planning to fail
Hi MadhiOnce again thanks for your replyI tried Your above query already ,but i am not getting the correct result |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-05 : 08:52:43
|
| declare @User Table(UserId int, Interests varchar(100))insert into @Userselect 1, '2,3,4,10' union allselect 2, '1,5,6,9' union allselect 3, '0,11,12,6,9,8'declare @Events Table (EventId int,Interests varchar(100))insert into @Events select 1, '0,8,6,7' union allselect 2, '1,9,12,15,10' union allselect 3, '8,9,6,5,2' select e.* from @user as u inner join @events as e on ','+u.interests+',' like '%,'+cast(eventid as varchar(10))+',%'where u.UserId=1MadhivananFailing to plan is Planning to fail |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-05 : 09:34:45
|
| Hi MadhiI am getting the correct result when i execute your query .But for the below query ,i am getting wrong answer.For that i need the eventid 1 details also since 10 is there.declare @User Table(UserId int, Interests varchar(100))insert into @Userselect 1, '2,3,4,10' union allselect 2, '1,5,6,9' union allselect 3, '0,11,12,6,9,8'declare @Events Table (EventId int,Interests varchar(100))insert into @Eventsselect 1, '0,8,6,7,10' union allselect 2, '1,9,12,15,10' union allselect 3, '8,9,6,5,2'select e.* from @user as u inner join @events as e on ','+u.interests+',' like '%,'+cast(eventid as varchar(10))+',%'where u.UserId=1Thanks |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2009-08-05 : 12:09:10
|
The query is all wrong. You need to compare Intrests to Intrests not Intrests to EventIDs.I think you'll need a string parsing function (there are several you can choose from). Soemthing like this should work:SELECT *FROM dbo.fnc_ParseString((SELECT Interests FROM @User WHERE UserID = 1), ',') AS AINNER JOIN @Events AS E ON ','+ E.interests + ',' LIKE '%,' + A.StringValue + ',%' |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-06 : 01:39:54
|
quote: Originally posted by Lamprey The query is all wrong. You need to compare Intrests to Intrests not Intrests to EventIDs.I think you'll need a string parsing function (there are several you can choose from). Soemthing like this should work:SELECT *FROM dbo.fnc_ParseString((SELECT Interests FROM @User WHERE UserID = 1), ',') AS AINNER JOIN @Events AS E ON ','+ E.interests + ',' LIKE '%,' + A.StringValue + ',%'
Hi Thanks for your reply I am getting error when i execute your query.Thanks in Advance |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-06 : 02:14:48
|
quote: Originally posted by Lamprey The query is all wrong. You need to compare Intrests to Intrests not Intrests to EventIDs.I think you'll need a string parsing function (there are several you can choose from). Soemthing like this should work:SELECT *FROM dbo.fnc_ParseString((SELECT Interests FROM @User WHERE UserID = 1), ',') AS AINNER JOIN @Events AS E ON ','+ E.interests + ',' LIKE '%,' + A.StringValue + ',%'
You should also apply the same function to @Events table tooMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|