| Author |
Topic |
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-14 : 03:11:34
|
| Hi Friends,I have two tables one is Books and Other one is BooksFeatured Books table hasBookId Userid Name Featured1 2 AASA 12 3 ASAB 13 8 CASA 04 3 SSSD 15 7 ASAS 06 1 FAAS 1BooksFeatured hasBFId UserId BookId FeaturedIn1 3 4 G2 1 6 NoWhat i need is ,i need the books which are all featured (Featured=1)with Featuredin =G from Booksfeaured table and which are all not featured (Featured=0)Expected OutBookId Userid Name Featured3 8 CASA 04 3 SSSD 15 7 ASAS 0Since bookid 4 is featured and featuredin in 'G' ,Bookid 3,5 are not featuredI tried with the below queryselect * from BooksFeatured BFRight join Books B on BF.BookId=B.BookId where BF.FeaturedIn='G' order by B.BookId descIf you need more explanation,i will explainThanks in Advance |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-14 : 05:33:17
|
| Hello Friends,Please reply me about this query.Your help will be much appreciated Thanks |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-14 : 05:49:11
|
| select t.* from Books t inner join BooksFeatured t1 on t1.userid = t.userid and t1.bookid = t.bookidand t1.FeaturedIn = 'g'union select t.* from Books t where Featured = 0 |
 |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2009-08-14 : 05:52:51
|
[code]DECLARE @Books TABLE (BookId INT, Userid INT, Name VARCHAR(4), Featured INT)INSERT INTO @Books SELECT 1,2,'AASA',1 UNION ALLSELECT 2,3,'ASAB',1 UNION ALLSELECT 3,8,'CASA',0 UNION ALLSELECT 4,3,'SSSD',1 UNION ALLSELECT 5,7,'ASAS',0 UNION ALLSELECT 6,1,'FAAS',1DECLARE @BooksFeatured TABLE (BFId INT, UserId INT, BookId INT, FeaturedIn VARCHAR(2))INSERT INTO @BooksFeaturedSELECT 1, 3, 4, 'G' UNION ALLSELECT 2, 1, 6, 'No'SELECT b.BookId, b.Userid, b.Name, b.FeaturedFROM @Books b left join @BooksFeatured f on b.userid=f.userid and b.bookid=f.bookidWHERE b.Featured=0 or f.FeaturedIn='G'[/code] Hope can help...but advise to wait pros with confirmation... |
 |
|
|
Kotti
Posting Yak Master
129 Posts |
Posted - 2009-08-14 : 06:02:28
|
| Hi waterduckThanks for your help.I got the answer with help of your queryYour help will be helpfull for me.Once again thanksThanks for bklr tooBklr when i use union i can't able to use order by |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-08-15 : 01:40:33
|
| [code]hi, u can use order by clause when u use union statementDECLARE @Books TABLE (BookId INT, Userid INT, Name VARCHAR(4), Featured INT)INSERT INTO @Books SELECT 1,2,'AASA',1 UNION ALLSELECT 2,3,'ASAB',1 UNION ALLSELECT 3,8,'CASA',0 UNION ALLSELECT 4,3,'SSSD',1 UNION ALLSELECT 5,7,'ASAS',0 UNION ALLSELECT 6,1,'FAAS',1DECLARE @BooksFeatured TABLE (BFId INT, UserId INT, BookId INT, FeaturedIn VARCHAR(2))INSERT INTO @BooksFeaturedSELECT 1, 3, 4, 'G' UNION ALLSELECT 2, 1, 6, 'No'select t.* from @Books t inner join @BooksFeatured t1 on t1.userid = t.userid and t1.bookid = t.bookidand t1.FeaturedIn = 'g'unionselect t.* from @Books t where Featured = 0order by bookid desc[/code] |
 |
|
|
|
|
|