从table_b向table_a部分插入值,从参数部分插入值? [英] Inserting values to table_a part from table_b and part from parameters?

查看:83
本文介绍了从table_b向table_a部分插入值,从参数部分插入值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候小伙伴们,我在这里偶然发现了一个棘手的问题,我希望在1个命令文本中实现这一点,想象一下,我有一个带有一些模板数据的table_a,所以我可以用所述模板数据填充table_b,另外2个列引用ID作为参数,例如:



Greetings guys, ive stumbled upon a tricky problem here, i want to achieve this in under 1 command text, imaginee i have a table_a with some template data so i can fill table_b with said template data and 2 more column referencing the IDs as parameters, for example:

SET @Last_ID=LAST_INSERTED_ID(); INSERT INTO table_a (col1, col2, col3) SELECT (col1,@Last_ID, col3) FROM table_b;

< br $> b $ b



这种事情是否可以实现?

如果您需要更多说明,请在下面留言。

最好的问候,

Eugen。



我尝试了什么:



我知道我可以通过2个单独的命令来完成它,将table_b中的所有数据导入程序中的某些数据表,然后通过垃圾邮件插入子句将数据插入table_a行示例:




Is this sort of thing even achievable?
Please leave comment below if you guys need more clarifications.
Best Regards,
Eugen.

What I have tried:

I know i can do it by 2 separate commands, get all the data from table_b into program into some datatable and then inserting the data into table_a by spamming the insert clause per line example:

SELET col1, col2 from table_b WHERE col3=@col3ParameterValue;
Insert into table_a (col1, col2, col3) Values (@col1_1, @col2_1, @col3_1), (@col1_2, @col2_2, @col3_2), (@col1_3, @col2_3, @col3_3), (@col1_4, @col2_4, @col3_4), (@col1_5, @col2_5, @col3_5), (@col1_6, @col2_6, @col3_6), (@col1_7, @col2_7, @col3_7), (@col1_8, @col2_8, @col3_8), (@col1_9, @col2_9, @col3_9)...



但这是不合理的,我不想诉诸这种方法。


but this is unneficient and i do not want to resort to this method.

推荐答案

如评论中所述,我建议您指定要插入的列。这可能不是MySql的确切语法,但在MSSql中它可以工作:



As mentioned in the comments, I would suggest that you specify your columns that you are inserting into. This may not be exact syntax for MySql but in MSSql it works:

INSERT INTO table_a (col1, col2, col3)
SELECT table_b.col1, @Last_ID, table_b.col2
FROM table_b
WHERE table_b.col3=@some_Other_Parameter


这篇关于从table_b向table_a部分插入值,从参数部分插入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆