|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Help, select multiple values on a listI have a question as follows: i have a look up table, which contains these values. lookup_id 1 2 3 4 5 Let say, i have another table which has record that has 1,2,3,4 references from this look up table. if i want to search for this record which contains 1 or 2, i could use something like SELECT..... WHERE .... lookup_id IN (1,2). What do i need to do if i want to select both 1 and 2 from this list? lookup_id = 1 AND lookup_id = 2 would not work obviously. I am lost, any pointers that can help? Thank you in advance. Fred You haven't posted any DDL, so we can't give you a coded solution. However,
if you Google "Relational Division", you may fins what you need. -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Columnist, SQL Server Professional Toronto, ON Canada www.pinpub.com .. <silver_celic***@yahoo.com> wrote in message news:1126831601.029710.39070@g49g2000cwa.googlegroups.com... I have a question as follows:Hi all, i have a look up table, which contains these values. lookup_id 1 2 3 4 5 Let say, i have another table which has record that has 1,2,3,4 references from this look up table. if i want to search for this record which contains 1 or 2, i could use something like SELECT..... WHERE .... lookup_id IN (1,2). What do i need to do if i want to select both 1 and 2 from this list? lookup_id = 1 AND lookup_id = 2 would not work obviously. I am lost, any pointers that can help? Thank you in advance. Fred Here is the DDL
TABLE_1 lookup_id int primary key, desc varchar(100) TABLE_2 id int primary key lookup_id int foreign key references TABLE 1.lookup_id So the question is how can search for anything in TABLE_2 which both lookup_id 1 AND 2. that is both 1 and 2 (or more, but 1 and 2 MUST exist) in TABLE_2 for the one record? How do i contruct such a SQL? Thank in advance. Fred You may be missing a column in TABLE_2. When you refer to "anything in
TABLE_2 ...", what do you mean by "anything"? What do you mean by "for the one record"? Do you simply want you check for the existence of both 1 and 2? If so, try: if ( select Count (*) from TABLE_2 where lookup_id in (1, 2) ) = 2 print 'Hit' else print 'Miss' -- Tom ---------------------------------------------------- Thomas A. Moreau, BSc, PhD, MCSE, MCDBA SQL Server MVP Columnist, SQL Server Professional Toronto, ON Canada www.pinpub.com .. <silver_celic***@yahoo.com> wrote in message news:1126833000.090229.182390@z14g2000cwz.googlegroups.com... Here is the DDLTABLE_1 lookup_id int primary key, desc varchar(100) TABLE_2 id int primary key lookup_id int foreign key references TABLE 1.lookup_id So the question is how can search for anything in TABLE_2 which both lookup_id 1 AND 2. that is both 1 and 2 (or more, but 1 and 2 MUST exist) in TABLE_2 for the one record? How do i contruct such a SQL? Thank in advance. Fred |
|||||||||||||||||||||||