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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Using AND operator on same column

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2005-04-08 : 08:32:12
Tyron writes "Dear SQL team

I would like to perform a query on a table with following values

table: qry_book_categories

CategoryID Category BookID Book
---------- --------- ------ ----
1 Pets 1 Dog training
2 Music 2 Piano lessons
3 Training 1 Dog training
1 Pets 1 Pigeons


I would like to view books that must belong to categories I specify e.g

Select BookID where CategoryID=1 AND CategoryID=3

The book Dog training should be returned since it belongs to both categories. I would very much appreciate your assistance.

Thank you in advance.
Tyron"

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-04-08 : 09:12:41
change your AND to OR.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Kaleem021
Starting Member

26 Posts

Posted - 2005-04-08 : 09:30:08
Select BookId from qry_book_categories t1 where CategoryId=1
and (select count(*) from qry_book_categories where CategoryId=3 and
BookId=t1.BookId) > 0

*****************************************************************************
Myth Breaker
Kaleem021@hotmail.com

Doing Nothing Is Very Hard To Do, You Never Know When You Are Finished.
Go to Top of Page

DustinMichaels
Constraint Violating Yak Guru

464 Posts

Posted - 2005-04-08 : 09:32:57
If possible you may want to normalize this table to eliminate some of the duplicate data.

Book
BookID
Book

Category
CategoryID
Category

BookCategory
BookID
CategoryID

Dustin Michaels
Go to Top of Page
   

- Advertisement -