我可以在 INSERT 语句中使用别名吗?

发布于 2024-09-19 11:20:37 字数 344 浏览 3 评论 0原文

我们可以在 insert into 语法中使用别名吗?

以下均无效:

INSERT INTO tableblabla AS bla
INSERT INTO bla tableblabla
INSERT INTO tableblabla bla

我似乎找不到任何有关此的信息;有没有在 INSERT 语句中使用别名的有效方法?

关于可能的原因: http://bugs.mysql.com/bug.php?id=3275

Can we use aliases with insert into syntax?

None of the following work:

INSERT INTO tableblabla AS bla
INSERT INTO bla tableblabla
INSERT INTO tableblabla bla

I can't seem to find any information about this; is there a valid way to use aliases in an INSERT statement?

About the possible reasons:
http://bugs.mysql.com/bug.php?id=3275

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

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

发布评论

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

评论(2

阳光下慵懒的猫 2024-09-26 11:20:38

唉,这是不可能的,正如 INSERT 语法 所示。注意它只是说[INTO] tbl_name而没有提及[AS]别名,而更新语法,它允许别名,表示“table_reference”和 进一步文档指出这可以扩展到table_factor,其中包括tbl_name [AS]别名

不幸的是,因为能够在 INSERT 上使用表别名对于 INSERT ... ON DUPLICATE KEY UPDATE 语句。特别是在对列执行检查时,如本示例所示:

insert into very_long_table_name_that_can_be_modified_one_day_or_during_testing (
    mykey, 
    column1,
    column2,
    column3,
    <column99>
)
select
    mykey, 
    column1,
    column2,
    column3,
    <column99>
from subquery
on duplicate key update 
    column1 = ifnull(values(column1), very_long_table_name_that_can_be_modified_one_day_or_during_testing.column1), 
    column2 = ifnull(values(column2), very_long_table_name_that_can_be_modified_one_day_or_during_testing.column2), 
    column3 = ifnull(values(column3), very_long_table_name_that_can_be_modified_one_day_or_during_testing.column3), 
    <column99>;

如果表名称发生更改,则与在查询中使用的开头有一个别名相比,必须修改大量代码行。

Alas, it's not possible as can be seen by the INSERT syntax. Note how it just says [INTO] tbl_name with no mention of [AS] alias, whereas the Update syntax, which does allow aliases, says "table_reference" and further documentation states this can expand to table_factor which includes tbl_name [AS] alias.

It's unfortunate because having the ability to use table aliases on INSERTs would be very useful for INSERT ... ON DUPLICATE KEY UPDATE statements. Especially when performing checks on the columns like in this example:

insert into very_long_table_name_that_can_be_modified_one_day_or_during_testing (
    mykey, 
    column1,
    column2,
    column3,
    <column99>
)
select
    mykey, 
    column1,
    column2,
    column3,
    <column99>
from subquery
on duplicate key update 
    column1 = ifnull(values(column1), very_long_table_name_that_can_be_modified_one_day_or_during_testing.column1), 
    column2 = ifnull(values(column2), very_long_table_name_that_can_be_modified_one_day_or_during_testing.column2), 
    column3 = ifnull(values(column3), very_long_table_name_that_can_be_modified_one_day_or_during_testing.column3), 
    <column99>;

If the table name changes, one has to modify lots of lines of code compared to just having an alias at the beginning which is used in the query.

寄居者 2024-09-26 11:20:38

INSERT 语法 不允许别名。无论如何,为什么在 INSERT 语句中需要一个呢?您一次只能 INSERT 到一张表中。

The INSERT syntax doesn't allow for aliases. Why would you need one in an INSERT statement anyways? You can only INSERT into one table at a time.

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