tdjfdjdj
tdjfdjdj

Reputation: 2471

sql - select row id based on two column values in same row as id

Using a SELECT, I want to find the row ID of 3 columns (each value is unique/dissimilar and is populated by separate tables.) Only the ID is auto incremented.

I have a middle table I reference that has 3 values: ID, A, B.

How can I select the row ID when I only know the value of A and B, and A and B are not the same value?

Upvotes: 1

Views: 1640

Answers (3)

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115540

It's not very clear. Do you mean this:

SELECT ID
FROM middletable
WHERE A = knownA
  AND B = knownB

Or this?

SELECT ID
FROM middletable
WHERE A = knownA
  AND B <> A

Or perhaps "I know A" means you have a list of values for A, which come from another table?

SELECT ID
FROM middletable
WHERE A IN
        ( SELECT otherA FROM otherTable ...)
  AND B IN
        ( SELECT otherB FROM anotherTable ...)

Upvotes: 1

fdaines
fdaines

Reputation: 1236

SELECT ID FROM table WHERE A=value1 and B=value2

Upvotes: 1

recursive
recursive

Reputation: 86084

Do you mean that columns A and B are foreign keys?

Does this work?

SELECT [ID] 
FROM tbl 
WHERE A = @a AND B = @b

Upvotes: 2

Related Questions