我想使用table_1选项中的(id_1)id数组来获取table_2中id_2 = id_1的所有行
SELECT field_b
FROM table_2
WHERE id_2 IN (
SELECT id_1 FROM table_1 WHERE field_a = 1234
);
以下是我的表格示例供参考:
mysql> select * from table_1;
+-------+---------+
| id_1 | field_a |
+-------+---------+
| 1 | 1234 |
+-------+---------+
| 2 | 1234 |
+-------+---------+
mysql> select * from table_2;
+---------+---------+
| id_2 | field_b |
+---------+---------+
| 1 | 5678 |
+-------+-----------+
| 2 | 0013 |
+-------+-----------+
答案 0 :(得分:3)
INNER JOIN对你来说效果会好吗?
SELECT field_b
FROM table_2
INNER JOIN table_1
ON table_2.id_2 = table_1.id_1
WHERE table_2.field_a=1234
答案 1 :(得分:1)
我不知道这是否是您所需要的:
SELECT * FROM table_2 t2
INNER JOIN table_1 t1
ON t2.id_2 = t1.id_1
WHERE t1.field_a = 1234
答案 2 :(得分:1)
另一种选择:
SELECT field_b
FROM table_2
WHERE EXISTS
( SELECT *
FROM table_1
WHERE field_a = 1234
AND table_2.id_2 = table_1.id_1
)