我非常感谢对表间的SQL查询提供一些帮助.我意识到这种事情是经常被问到的,但我找不到类似的问题来理解答案.
我想从中选择table_A具有相应标签的行table_B.
因此,例如,"选择table_a标记'主席'的行将返回" 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
Run Code Online (Sandbox Code Playgroud)
或者,是否有更好的方法来组织数据?
Chr*_*ava 96
最简单的解决方案是相关的子选择:
select
A.*
from
table_A A
where
A.id in (
select B.id from table_B B where B.tag = 'chair'
)
Run Code Online (Sandbox Code Playgroud)
或者,您可以连接表并过滤所需的行:
select
A.*
from
table_A A
inner join table_B B
on A.id = B.id
where
B.tag = 'chair'
Run Code Online (Sandbox Code Playgroud)
您应该对两者进行分析,并查看数据集中哪个更快.
您应该使用链接表为自己的表创建标记.
items:
id object
1 lamp
2 table
3 stool
4 bench
tags:
id tag
1 furniture
2 chair
items_tags:
item_id tag_id
1 1
2 1
3 1
4 1
3 2
4 2
Run Code Online (Sandbox Code Playgroud)