0

我有这样的表“用户”

id  | firstname | lastname
==========================
1   | Ujang     | ahmad
2   | Jajat     | sudrajat

并有数据:

$record = array('firstname'=>'some value', 'lastname'=>'some value');
$table  = "users";

并像这样处理更新:

$exc= $conn->AutoExecute($table, $record, 'UPDATE', 'id = 1');

如何使用自动执行的姓氏值更新字段名字

所以我得到这样的结果:

id  | firstname | lastname
==========================
1   | ahmad     | Ujang
2   | sudrajat  | Jajat
4

1 回答 1

0

除非我误解了你,否则 AutoExecute 似乎不适合这项工作。如果您需要对表中的所有记录进行一次性转换,我只需重命名这些列。

ALTER TABLE `users`
CHANGE COLUMN `lastname` `firstname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `id`,
CHANGE COLUMN `firstname` `lastname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `firstname`;

或者在 PHP/ADODB 中:

$sql = "ALTER TABLE `users`
  CHANGE COLUMN `lastname` `firstname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `id`,
  CHANGE COLUMN `firstname` `lastname`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL AFTER `firstname`;";
if (($result = $conn->Execute($sql)) === false)
  exit($sql.'<br />'.$conn->errorMsg());

如果您需要针对特定​​记录,您可以使用临时变量。

$sql = "UPDATE users
  SET firstname=(@temp:=firstname), firstname = lastname, lastname = @temp
  WHERE id=2";
if (($result = $conn->Execute($sql)) === false)
  exit($sql.'<br />'.$conn->errorMsg());

干杯

于 2014-04-17T11:50:14.910 回答