40

I have two MySQL tables a and b with fields x and y. Table b has 1 extra field z. Table a is in database db1 and b is in db2. I want to copy x and y from a to b and set a static value for z. How can I do that ?

db1.a.x -> db2.b.x
db1.a.y -> db2.b.y
4 -> db2.b.z

So far I have:

"INSERT INTO db2.b (x,y) SELECT x,y FROM db1.a"

How do I set db2.b.z to 4 ? I do not want to set a permanent default variable for the table.

2 Answers 2

91

SELECT 4 will give you 4, so try:

INSERT INTO db2.b (x,y,z) SELECT x,y,4 FROM db1.a
Sign up to request clarification or add additional context in comments.

1 Comment

Since this is the top result on Google: you can do strings, too, but not if they match a table or a column or any such identifier, even when quoted.
16
INSERT INTO db2.b (x, y, z) SELECT x, y, 4 FROM db1.a;

Comments

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.