MySQL从表到表复制行,同时使用PHP

时间:2013-04-25 08:48:41

标签: php mysql rows

我知道你可以这样做:

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

但是,如果不是SELECT a, b, c, d,则需要将这些列更新为a, b, c, "string"

所以说白了,我想要一个声明:

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

更新

所以我跑了这个:

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

它在'c'列中选择了3行不同的条目,并通过插入3行完全相同来更新'AnotherTable'。虽然它们应该是不同的值......

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

但是,你应该非常小心地注入$ somevar。你真的应该使用准备好的陈述,所以它会......

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

和CustomValue通过将值绑定到语句来设置。这里有一些例子:http://www.php.net/manual/en/pdostatement.bindvalue.php