简体   繁体   中英

MySQL Copying row from table to table, using PHP at same time

I know you can do this:

INSERT INTO AnotherTable (a, b, c, d)
SELECT a, b, c, d
FROM SomeTable
WHERE SomeColumn = 1

But what if instead of SELECT a, b, c, d you need to update those columns as a, b, c, "string"

so to put it plainly I want a statement that says:

INSERT INTO AnotherTable (a, b, c, d)
SELECT a, b
FROM SomeTable
WHERE SomeColumn = 1
// And put  "string" into d

UPDATE

So I ran this:

INSERT INTO AnotherTable (a, b, c, d)
SELECT a, b, c, 'string'
FROM SomeTable
WHERE SomeColumn = 1

It selected 3 rows with different entries in 'c' column and it updates 'AnotherTable' by inserting 3 rows all the exact same. They should be different values though...

INSERT INTO AnotherTable (a, b, c, d)
SELECT a, b,"SOME VALE","SOMEVALUE2"
FROM SomeTable
WHERE SomeColumn = 1
INSERT INTO AnotherTable (a, b, c, d)
SELECT a, b, $somevar, 'fixed string'
FROM SomeTable
WHERE SomeColumn = 1

However, you should be very careful about injecting $somevar like that. Really you should be using prepared statements, so it would be...

INSERT INTO AnotherTable (a, b, c, d)
SELECT a, b, :CustomValue, 'fixed string'
FROM SomeTable
WHERE SomeColumn = 1

and CustomValue is set by binding the value to the statement. There are some examples here: http://www.php.net/manual/en/pdostatement.bindvalue.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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