Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

multiple column subquery

Posted on 2002-05-06
3
Medium Priority
?
4,131 Views
Last Modified: 2010-04-27
Greetings,

Can someone point me to an equivalent statement that will run on both
ms sql server 7 and 2000?

I am trying to select some rows from a table where two column entries
match some other criteria. The Oracle equivalent is at the bottom of
this message... In MS SQL Server Query Analyzer I am getting errors
trying this:

        SELECT table1.col1, table1.col2, table1.col3
        FROM table1
        WHERE (table1.num, table1.code) in
          (SELECT table2.num, table2.code from table2
          where table2.col4 = value


The error is: "Incorrect Syntax near ',' ", so it seems to not like
the comma in "where (table1,num, table2.code) in"

The important thing to remember is that each row in one table will have the same two values as a row in the other table.

.. I did not want to concatenate the rows as it seems to deliver different results.

Thanks for any assistance,
Jeff

select (col1, col2, col3
from table1
where (num, code) in
(select num, code from table2 where col4 = value);

0
Comment
Question by:kishj
3 Comments
 
LVL 3

Accepted Solution

by:
trouta earned 150 total points
ID: 6991723
The where exists clause will need to be used if you have multiple values to check for

SELECT table1.col1, table1.col2, table1.col3
FROM table1
WHERE EXISTS (SELECT * FROM TABLE2 WHERE table1.num = table2.num and table1.code = table2.code and table2.col4 = value)

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 6993429
You can also use a join to resolve this, like so:

SELECT table1.col1, table1.col2, table1.col3
    FROM table1
    INNER JOIN table2 ON table1.num = table2.num AND table1.code = table2.code
    WHERE table2.col4 = value

0
 
LVL 1

Expert Comment

by:usmanr
ID: 32265628
extending trouta's solution, you can also write it this way,,
select * from Address where exists (select 1 where City = 'Bothell' and AddressLine1 = '1970 Napa Ct.')
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question