子查询为MySQL中的生成列?

发布于 2025-01-31 03:19:37 字数 724 浏览 4 评论 0原文

我可以在表A中创建一个生成的列,该列在表B中使用表A中的tablea_id总结一个列?

假设我有一张家庭表和一张孩子桌。我想要每个家庭的孩子的年龄。

ALTER TABLE people.families 
ADD COLUMN sumofages DECIMAL(10,2) GENERATED ALWAYS AS 
(SELECT SUM(age) FROM people.children WHERE family_id = people.families.id) STORED;

ERROR 3102: Expression of generated column 'sumofages' contains a disallowed function.

我也无法将其保存为虚拟的类型。我在这里做错了什么?

ALTER TABLE people.families 
ADD COLUMN sumofages DECIMAL(10,2) GENERATED ALWAYS AS 
(SELECT SUM(age) FROM people.children WHERE family_id = people.families.id) VIRTUAL;

ERROR 3102: Expression of generated column 'sumofages' contains a disallowed function.

我不知道哪个功能不允许。总和似乎不是它。也许选择?

Can I create a generated column in table A which sums up a column in table B with a tableA_id of the row in table A?

Suppose I have a table of of families, and a table of children. I want a sum of the ages of the children for each family.

ALTER TABLE people.families 
ADD COLUMN sumofages DECIMAL(10,2) GENERATED ALWAYS AS 
(SELECT SUM(age) FROM people.children WHERE family_id = people.families.id) STORED;

ERROR 3102: Expression of generated column 'sumofages' contains a disallowed function.

I can't save it as type VIRTUAL either. What am I doing wrong here?

ALTER TABLE people.families 
ADD COLUMN sumofages DECIMAL(10,2) GENERATED ALWAYS AS 
(SELECT SUM(age) FROM people.children WHERE family_id = people.families.id) VIRTUAL;

ERROR 3102: Expression of generated column 'sumofages' contains a disallowed function.

I don't know which function is disallowed. SUM doesn't seem to be it. Maybe SELECT?

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

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

发布评论

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

评论(2

高冷爸爸 2025-02-07 03:19:37

https://dev.mysql.com/doc /refman/5.7/en/create-table-generated-columns.html

生成的列表达式必须遵守以下规则。一个
如果表达式包含不允许的构建体,则会发生错误。

  • 子查询,参数,变量,存储的功能和用户定义
    不允许使用功能。

合理的是生成列的表达式可以在同一行中引用列。生成的列无法使用子征服或引用其他表格的函数,具有非确定性输出的功能。

假设生成的列确实支持跨表参考。特别考虑存储的情况生成的列。

如果更新表,则MySQL还必须更新数据库中其他位置中生成的列中的任何引用,如果它们引用了您更新的行。对于MySQL来说,追踪所有这些参考资料将是复杂且昂贵的。

然后考虑通过存储的功能添加间接参考。

然后考虑您的更新是交易中的InnoDB表,但是生成的列可能是在非交易中(Myisam,内存,存档等)表中。制作时,是否应该在那些生成的列中反映您的更新?如果您回头怎么办?您提交时是否应该反映您的更新?那么MySQL应该如何更改将其应用于这些表?如果多个交易提交影响生成的列参考的更新怎么办?哪一个应该赢,最后一次应用更改或最后一次犯下的更改?

由于这些原因,允许生成的列在同一表中引用同一行的列以外的任何内容是不切实际的或有效的。

https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html

Generated column expressions must adhere to the following rules. An
error occurs if an expression contains disallowed constructs.

  • Subqueries, parameters, variables, stored functions, and user-defined
    functions are not permitted.

It's reasonable that the expression for a generated column can reference only columns within the same row. The generated column cannot use subqueries, or reference other tables, or functions with non-deterministic output.

Suppose generated columns did support cross-table references. Particularly consider the case of STORED generated columns.

If you update a table, MySQL would also have to update any references in generated columns elsewhere in the database, if they reference the row you updated. It would be complex and expensive for MySQL to track down all those references.

Then consider add indirect references through stored functions.

Then consider that your update is to an InnoDB table in a transaction, but the generated column may be in a non-transaction (MyISAM, MEMORY, ARCHIVE, etc.) table. Should your update be reflected in those generated columns when you make it? What if you roll back? Should your update be reflected at the time you commit? Then how should MySQL "queue up" changes to apply to those tables? What if multiple transactions commit updates that affect the generated column reference? Which one should win, the one that applied the change last or the one that committed last?

For these reasons, it's not practical or efficient to allow generated columns to reference anything other than the columns of the same row in the same table.

空心空情空意 2025-02-07 03:19:37

计算列的想法是从记录中的其他列中得出数据,例如将国家代码与邮政编码结合在一起,因此您将存储DE和12345,并且您将获得DE-12345,您可以在地址。

但是,您试图做的事情是完全不同的。您正在从另一个表访问数据。但是该表的数据可能会发生变化,因此当访问相同的记录时,您可能会突然获得完全不同的结果。计算的列应包含确定性值,因此只要您的记录的数据不更改,它们就不会更改。在这方面,我不知道MySQL,但是它可能禁止非确定性数据,例如您的子查询。

您实际寻找的是一个视图。视图可以从不同的表中组合选择,就像您想发生的那样。因此,请使用

create view familydata as
(
  select f.*, sum(c.age) as sumofages
  from families f
  join children c on c.family_id = f.id
  group by f.id
);

,或者

create view familydata as
(
  select f.*,
  (
    select sum(age)
    from children c
    where c.family_id = f.id
  ) as sumofages
  from families f
);

我希望我能正确理解语法。

The idea of a computed column is to derive data from the other columns in the record, e.g. combine the country code with the zip code, so you'd store DE and 12345 and you'd get DE-12345 which you could use in an address.

What you are trying to do, however, is something completely different. You are accessing data from another table. But that table's data may change, so when accessing the same record you might suddenly get a completely different result. Computed columns should contain deterministic values, so they don't change as long as your record's data doesn't change. I don't know about MySQL in this regard, but it probably forbids non-deterministic data, such as your subquery.

What you are actually looking for is a view. A view can combine selects from different tables, just as you want it to happen. So use

create view familydata as
(
  select f.*, sum(c.age) as sumofages
  from families f
  join children c on c.family_id = f.id
  group by f.id
);

or

create view familydata as
(
  select f.*,
  (
    select sum(age)
    from children c
    where c.family_id = f.id
  ) as sumofages
  from families f
);

I hope I got the syntax right.

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