在 SQL 中同步跨表的 N 个值

发布于 2024-08-01 16:06:27 字数 686 浏览 5 评论 0原文

免责声明:我刚刚开始使用数据库,所以我可能错过了一些非常微不足道的东西。

我有两个(有些相关,但不足以成为单个表)表:table_onetable_two


table_one 有两列重要的内容:

  1. name 类型为 varchar(n)
  2. intersects 类型为 bit< /code>

table_two 有一列重要:

  1. name 类型为 varchar(n)

我想自动intersects 设置为 01,具体取决于 name 是否出现在 table_two 中。

我可以以某种方式将这个责任委托给数据库引擎吗? 我目前正在使用 MySQL,如果这很重要的话。


编辑:在处理数据库时这种紧密耦合是否有意义?

Disclaimer: I just started with databases, so I'm probably missing something extremely trivial.

I have two (somewhat related, but not enough to be a single table) tables: table_one and table_two.


table_one has two columns of significance:

  1. name of type varchar(n)
  2. intersects of type bit

table_two has one column of significance:

  1. name of type varchar(n)

I would like to automatically set intersects to either 0 or 1 depending on whether name is present in table_two.

Can I somehow delegate this responsibility to the database engine? I'm currently using MySQL, if that matters.


EDIT: Does this sort of close-coupling even make sense when dealing with databases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

故人爱我别走 2024-08-08 16:06:27

ANSI SQL:

update table_one
set intersects = 
    case when 
        (select count(*) from table_two where name = table_one.name) > 0 
        then 1 
    else 0 end

MySQL Join:

update table_one t1
    left join table_two t2 on
        t1.name = t2.name
set t1.intersects = case when t2.name is not null then 1 else 0 end

您可以将此查询放入 < code>trigger 使 intersects 保持最新:

create trigger intersections after insert for each row
begin

    update table_one t1
        left join table_two t2 on
            t1.name = t2.name
    set t1.intersects = case when t2.name is not null then 1 else 0 end
    where t1.name = new.name

end

现在,对于设计讨论:

intersects 列很棒,但是您可能会遇到不同步的问题。 您可以做的是在调用此函数时在 select 中使用 exists 函数:

select
    *
from
    table_one t1
where
    exists (select 1 from table_two t2 where t2.name = t1.name)

您可以使用 notexists 来查找不相交的那些,还有。

此外,您还可以使用 left join 来拉回这两种情况:

select
    *
from
    table_one t1
    left join table_two t2 on
        t1.name = t2.name

任何 t2.namenull 的情况,您都没有路口。 任何不存在的地方,你都会做。 当然,如果您对 name 没有 unique 约束,这可能会产生重复的行。

希望这能为所有这一切带来一些启发!

ANSI SQL:

update table_one
set intersects = 
    case when 
        (select count(*) from table_two where name = table_one.name) > 0 
        then 1 
    else 0 end

MySQL Join:

update table_one t1
    left join table_two t2 on
        t1.name = t2.name
set t1.intersects = case when t2.name is not null then 1 else 0 end

You can put this query into a trigger to keep intersects up-to-date:

create trigger intersections after insert for each row
begin

    update table_one t1
        left join table_two t2 on
            t1.name = t2.name
    set t1.intersects = case when t2.name is not null then 1 else 0 end
    where t1.name = new.name

end

Now, for the design discussion:

An intersects column is great and all, but you may run into the issue where it gets out of sync. What you can do is use an exists function in your select when calling this:

select
    *
from
    table_one t1
where
    exists (select 1 from table_two t2 where t2.name = t1.name)

You can use not exists to find those that don't intersect, as well.

Moreover, you can also use a left join to pull back both cases:

select
    *
from
    table_one t1
    left join table_two t2 on
        t1.name = t2.name

Anything where t2.name is null, you don't have an intersection. Anything where it isn't, you do. Of course, this can produce duplicate rows if you don't have a unique constraint on name.

Hopefully this sheds some light on all of this!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文