Gopal
Gopal

Reputation: 11972

How to check 2 values in where condition

Using SQL Server

I want to check two values in two table.

I have two integer value (value1, value2)

Table1

ID Value

001 200
002 300
...

Table2

ID Value

001 300
002 400
...

Now I want to check whether value1 and value2 is matching with table1 value and table2 value

Tried Query

SELECT value from table1 Where  id = '" & textbox1.Text & "'

Condition

I want to check whether the value1 is matching with table1 or table2, then value2 is matching with table1 or table2. If it is matching then show the value otherwise null.

How to make a query.

Need Query help

Upvotes: 0

Views: 3211

Answers (3)

sys_debug
sys_debug

Reputation: 4003

Usually, I add the query as a string without concatenating it..

SELECT * FROM ...etc WHERE id=@id AND networktag=@networktag

then deal with ID using the parameter on command (i.e. an instance of SqlCommand in C#) this is mostly using c# and I think easy to do in VB

Hope this helps

Upvotes: 0

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58431

If I understand you correctly, you could use a UNION to accomplish this.

SELECT  *
FROM    (
          SELECT ID, Value, 'Table1' AS Source
          FROM   Table1
          UNION ALL
          SELECT ID, Value, 'Table2'
          FROM   Table2
        ) u
WHERE   u.Value IN (@Value1, @Value2)

Upvotes: 0

gbn
gbn

Reputation: 432210

SELECT
    MAX(T1.ID)
FROM
    (
    SELECT TOP 1 ID
    FROM table1 WHERE value = @Value1
    ORDER BY ID
    ) T1
    JOIN
    (
    SELECT TOP 1 ID
    FROM table2 WHERE value = @Value2
    ORDER BY ID
    ) T2 ON T1.ID = T2.ID

Notes:

For the last point, if you want any match

SELECT
   MAX(1)
FROM
    (
    SELECT TOP 1 ID
    FROM table1 WHERE value = @Value1
    ORDER BY ID
    ) T1
    CROSS JOIN
    (
    SELECT TOP 1 ID
    FROM table2 WHERE value = @Value2
    ORDER BY ID
    ) T2

Upvotes: 1

Related Questions