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.
- A is based on data from another table.
- B is based on data from another table.
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?
Do you mean that columns A and B are foreign keys?
Does this work?
SELECT [ID]
FROM tbl
WHERE A = @a AND B = @b
SELECT ID FROM table WHERE A=value1 and B=value2
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 ...)
精彩评论