简体   繁体   English

MySQL同时使用PHP在表格之间复制行

[英]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" 但是,如果要代替SELECT a, b, c, d而是将这些列更新为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. 它选择了“ c”列中具有不同条目的3行,并通过插入完全相同的3行来更新“ AnotherTable”。 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. 但是,您应该非常小心地注入这样的$ somevar。 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. 通过将值绑定到语句来设置CustomValue。 There are some examples here: http://www.php.net/manual/en/pdostatement.bindvalue.php 这里有一些示例: http : //www.php.net/manual/zh/pdostatement.bindvalue.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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