简体   繁体   English

在MySQL函数中从查询SELECT获取值

[英]Getting values from query SELECT in MySQL function

I have a function and i want get values from query SELECT . 我有一个函数,我想从查询SELECT获取值。

I want to do something like: obj = SELECT c1 , c2 , c3 FROM table1 where id=1 and next INSERT INTO table2 ( c1 , c2 , c3 , c4 , c5) VALUES(obj->c1, obj->c2, obj->c3, 's1', 's2') 我想做类似的事情: obj = SELECT c1 , c2 , c3 FROM table1 where id=1 and next INSERT INTO table2 ( c1 , c2 , c3 , c4 , c5) VALUES(obj->c1, obj->c2, obj->c3, 's1', 's2')

Of course it doesn't have to be an object it can be any variable. 当然,它不必是对象,也可以是任何变量。 Can u tell me how should i do this? 你能告诉我该怎么做吗? I'm just a beginner don't blame me :) 我只是一个初学者,不要怪我:)

Simply as this: 就这么简单:

INSERT INTO table2 (c1, c2, c3) 
SELECT c1, c2, c3 FROM table1 where id=1

More info here . 更多信息在这里

To do exactly what you ask: 要完全按照您的要求进行操作:

SELECT c1, c2, c3 INTO @c1, @c2, @c3 FROM table1 where id=1; 从table1中选择c1,c2,c3 INTO @ c1,@ c2,@ c3,其中id = 1;

INSERT INTO table2 (c1, c2, c3, c4, c5) VALUES(@c1, @c2, @c3, 's1', 's2'); 插入表2(c1,c2,c3,c4,c5)值(@ c1,@ c2,@ c3,'s1','s2');

But that is not necessarily the best approach, would need more understanding of exact requirement. 但这不一定是最好的方法,需要对确切要求有更多的了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM