0

I have a function and i want get values from query 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')

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 :)

2 Answers 2

2

Simply as this:

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

More info here.

Sign up to request clarification or add additional context in comments.

2 Comments

But if I want combine query like INSERT into table2 (c1, c2, c3, c4, c5) and for example c4=4 and c5=4. how should i do this then?
INSERT INTO table2 (c1, c2, c3, c4, c5) SELECT c1, c2, c3, "4", "5" FROM table1 where id=1
1

To do exactly what you ask:

SELECT c1, c2, c3 INTO @c1, @c2, @c3 FROM table1 where id=1;

INSERT INTO table2 (c1, c2, c3, c4, c5) VALUES(@c1, @c2, @c3, 's1', 's2');

But that is not necessarily the best approach, would need more understanding of exact requirement.

3 Comments

You mean when select returns more than 1 record or u talked about database efficiency?
this would only work for select returning one row and yes efficiency as this is creating variables etc. The other solution: INSERT INTO table2 (c1, c2, c3, c4, c5) SELECT c1, c2, c3, "4", "5" FROM table1 where id=1 would not create variables and accomplish the same thing, but depending on exactly how you are using it might not solve your problem.
Yes but if i want do the same into UPDATE already it isn't so easy as your solution, but whatever i can do LIMIT 1, and wont be any complications :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.