Insert / Select with different values ​​Possible in MYSQL?

I need to insert values ​​into db.

INSERT INTO tblname(f1,f2,f3)
SELECT f1,f2,f3 FROM othertbl 

      

This will insert exactly the values f1, f2, f3 from othertbl .

What I want to do is

Insert another value for f2.

How can I do this in MYSQL.

Hopefully I can explain my problem.

+2


source to share


4 answers


INSERT
INTO    tblname(f1,f2,f3)
SELECT  f1, othervalue, f3
FROM    othertbl 

      



+3


source


It really depends on what that value is ...

If you want to insert some known constant you can use:

INSERT INTO tblname(f1,f2,f3)
SELECT f1,"foo",f3 FROM othertbl 

      

If you want to insert a value in f2

, but twice, you can use:



INSERT INTO tblname(f1,f2,f3)
SELECT f1,f2*2,f3 FROM othertbl 

      

If you want to insert the amount f1

and f3

, you can use:

INSERT INTO tblname(f1,f2,f3)
SELECT f1,f1+f3,f3 FROM othertbl 

      

But I know these are probably the wrong functions for math, so I will need to find the right way to get my examples to work, but the idea should be clear.

+2


source


INSERT INTO tblname(f1,f2,f3)
SELECT f1,f2,f3 FROM othertbl 

      

0


source


U can use string or math functions mysql a / c ur needs

you can find them here

http://dev.mysql.com/doc/refman/5.1/en/string-functions.html

http://dev.mysql.com/doc/refman/5.1/en/non-typed-operators.html

0


source







All Articles