REPLACE INTO SQL语法错误

时间:2014-04-09 14:51:34

标签: mysql sql

我有一个REPLACE的SQL语法

  $query = "REPLACE INTO ".TBL_ACTIVE_ADMINS." VALUES (:username, :time, :userlevel)";
  $stmt = $this->connection->prepare($query);
  $stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));

这样可以正常工作但是我想稍微调整它以便在其中有一个where子句允许:userlevel为'9'我使用REPLACE SELECT尝试了这个但我一直收到错误

  $query = "REPLACE INTO ".TBL_ACTIVE_ADMINS."(:username, :time, :userlevel) SELECT * FROM ".TBL_USERS." WHERE userlevel = 9 SET username = :username and timestamp = :time and userlevel = :userlevel";
  $stmt = $this->connection->prepare($query);
  $stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));

有人可以向我解释我在上面的代码中做错了吗?

1 个答案:

答案 0 :(得分:0)

没关系;在改变代码的很多变化之后,我决定从不同的角度来看待它。我现在编写了一个函数,每次刷新页面时都会清理SQL表,因此可以准确计算我的站点上当前活动的人员。

我使用下面的代码,然后在调用count之前我清理了数据库表

$query = "REPLACE INTO ".TBL_ACTIVE_ADMINS." VALUES (:username, :time, :userlevel)";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));

所以我的结束代码看起来像这样

$query = "REPLACE INTO ".TBL_ACTIVE_ADMINS." VALUES (:username, :time, :userlevel)";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':username' => $username, ':time' => $time, ':userlevel' => $userlevel));

$query = "DELETE FROM ".TBL_ACTIVE_ADMINS." WHERE userlevel = 3";
$stmt = $this->connection->prepare($query);
$stmt->execute(array(':userlevel' => $userlevel));

$this->calcNumActiveAdmins();