Solved

multiple column subquery

Posted on 2002-05-06
3
4,118 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 50 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 69

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server Generate Scripts Fails 5 35
script to send html report 3 32
SQL Querying data from 3 tables, all with 1 common column 4 34
SQL Query assistance 16 22
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how the fundamental information of how to create a table.

776 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