简体   繁体   English

sql - select 行 id 基于同一行中的两个列值作为 id

[英]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.使用SELECT ,我想找到 3 列的行ID (每个值是唯一/不同的,并且由单独的表填充。)只有ID是自动递增的。

I have a middle table I reference that has 3 values: ID, A, B.我有一个我引用的中间表,它有 3 个值:ID、A、B。

  • A is based on data from another table. A 基于另一个表中的数据。
  • B is based on data from another table. 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?当我只知道 A 和 B 的值,而 A 和 B 的值不同时,我怎么能 select 行 ID?

Do you mean that columns A and B are foreign keys?你的意思是A列和B列是外键吗?

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?或者也许“我知道 A”意味着你有一个 A 的值列表,它来自另一个表?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM