views:

174

answers:

4

I need to insert values in to the db.

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

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

What i want to do is

Inserting a different value for f2.

How can i do that in MYSQL.

I hope i could explain my problem clear.

+2  A: 
INSERT
INTO    tblname(f1,f2,f3)
SELECT  f1, othervalue, f3
FROM    othertbl 
Quassnoi
A: 

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

Babar
+1  A: 

It really depends on what that different value is...

If you want to insert some known constant, you could use:

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

If you want to insert the value at f2 but double, you could use:

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

If you want to insert the sum of f1 and f3, you could use:

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

But I know those are probably the wrong functions to do the math, so I'll have to look up the right way to make my examples work, but the idea should be clear.

Anthony
A: 

U can use string or math functions of 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

nik