我非常感谢有关跨表 SQL 查询的帮助。我意识到这类问题经常被问到,但我找不到足够相似的问题来理解答案。
我想从 table_A
中选择在 table_B
中具有相应标记的行。
因此,例如,“从 table_a
中选择标记为“chair”的行”将返回 table_C
。
此外,id
在 table_a
中是唯一的,而不是在 table_b
中。
table_A: table_B: table_C:
id object id tag id object
1 lamp 1 furniture 3 stool
2 table 2 furniture 4 bench
3 stool 3 furniture
4 bench 4 furniture
4 chair
3 chair
或者,是否有更好的方法来组织数据?
最佳答案
最简单的解决方案是相关子选择:
select
A.*
from
table_A A
where
A.id in (
select B.id from table_B B where B.tag = 'chair'
)
或者,您可以加入表格并过滤所需的行:
select
A.*
from
table_A A
inner join table_B B
on A.id = B.id
where
B.tag = 'chair'
您应该对两者进行分析,看看哪个在您的数据集上速度更快。
关于sql - 从一张表中选择与另一张表匹配的条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5446778/