从一张表中选择与另一张表匹配的条件?

发布于 2024-10-26 15:06:24 字数 717 浏览 0 评论 0原文

我非常感谢有关跨表 SQL 查询的帮助。我意识到这类问题经常被问到,但我找不到足够相似的问题来理解答案。

我想从 table_A 中选择在 table_B 中具有相应标记的行。
因此,例如,“从 table_a 中选择标记为“chair”的行”将返回 table_C

此外,idtable_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里梦着梦中梦 2024-11-02 15:06:25

我也有类似的问题(至少我认为是相似的)。在此处的一个答复中,解决方案如下:

select
    A.*
from
    table_A A
inner join table_B B
    on A.id = B.id
where
    B.tag = 'chair'

我希望使用 WHERE 子句:

WHERE B.tag = A.<col_name>

或者,在我的具体情况下:

WHERE B.val BETWEEN A.val1 AND A.val2

更详细:

表 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:

select
    A.*
from
    table_A A
inner join table_B B
    on A.id = B.id
where
    B.tag = 'chair'

That WHERE clause I would like to be:

WHERE B.tag = A.<col_name>

or, in my specific case:

WHERE B.val BETWEEN A.val1 AND A.val2

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.

凤舞天涯 2024-11-02 15:06:24

最简单的解决方案是相关子选择

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'

您应该对两者进行分析,看看哪个在数据集上速度更快。

The simplest solution would be a correlated sub select:

select
    A.*
from
    table_A A
where
    A.id in (
        select B.id from table_B B where B.tag = 'chair'
)

Alternatively you could join the tables and filter the rows you want:

select
    A.*
from
    table_A A
inner join table_B B
    on A.id = B.id
where
    B.tag = 'chair'

You should profile both and see which is faster on your dataset.

杀手六號 2024-11-02 15:06:24

您应该使用链接表使标签成为自己的表。

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

You should make tags their own table with a linking table.

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
柳若烟 2024-11-02 15:06:24
select a.id, a.object
from table_A a
inner join table_B b on a.id=b.id
where b.tag = 'chair';
select a.id, a.object
from table_A a
inner join table_B b on a.id=b.id
where b.tag = 'chair';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文