【发布时间】:2020-08-07 16:22:36
【问题描述】:
我试图通过加入另一个表假设 table2 来获取表的行,假设 table2 的 ID 存在于 table1 的两列之一中,并在 table2 上应用 where 和 IsActive =1 这是 table2 表中的一列,但我无法获取正确的数据。
Table1如下:
table1_ID|column_x|column_y
---------+--------+---------
1 |29 |30
2 |30 |31
3 |31 |32
table2 是这样的:
table2_ID|IsActive
---------+--------
29 |1
30 |1
31 |1
32 |0
下面是我的查询:
SELECT
table2.[table2_id],
table2.[column_x],
table2.[[column_y]
FROM
[table2]
WHERE
(table2.[column_x] IN (SELECT table1_id FROM table1 WHERE IsActive = 1)
OR table2.[[column_y] IN (SELECT table1_id FROM table1 WHERE IsActive = 1))
但我得到了所有的行,但我期待的是:
table1_ID|column_x|column_y
---------+--------+---------
1 |29 |30
2 |30 |31
【问题讨论】:
标签: sql sql-server