从一张表中选择与另一张表匹配的条件?
我非常感谢有关跨表 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
或者,是否有更好的方法来组织数据?
I'd really appreciate some help with an SQL query across tables. I realise this sort of thing is asked constantly, but I can't find a similar enough question to make sense of the answers.
I want to select rows from table_A
that have a corresponding tag in table_B
.
So, for example, " select rows from table_a
which are tagged 'chair' " would return table_C
.
Also, id
is a unique in table_a
, and not in 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
Alternatively, is there a better way to organise the data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也有类似的问题(至少我认为是相似的)。在此处的一个答复中,解决方案如下:
我希望使用 WHERE 子句:
或者,在我的具体情况下:
更详细:
表 A 包含一组设备的状态信息。每个状态记录都带有该状态的开始和停止时间。表 B 包含有关设备的定期记录的带时间戳的数据,我想在表 A 中指示的时间段内提取这些数据。
I have a similar problem (at least I think it is similar). In one of the replies here the solution is as follows:
That WHERE clause I would like to be:
or, in my specific case:
More detailed:
Table A carries status information of a fleet of equipment. Each status record carries with it a start and stop time of that status. Table B carries regularly recorded, timestamped data about the equipment, which I want to extract for the duration of the period indicated in table A.
最简单的解决方案是相关子选择:
或者,您可以加入表并过滤所需的行:
您应该对两者进行分析,看看哪个在数据集上速度更快。
The simplest solution would be a correlated sub select:
Alternatively you could join the tables and filter the rows you want:
You should profile both and see which is faster on your dataset.
您应该使用链接表使标签成为自己的表。
You should make tags their own table with a linking table.