没有双重条目的PDO SQL语句

时间:2015-09-03 10:36:17

标签: php mysql

我正在尝试使PDO MYSQL语句只插入数据库中不存在的数据

PHP的简单示例

$sql = INSERT INTO mytable SET hash=:hash, tablename=:tablename, recuid=:recuid WHERE recuid NOT IN (SELECT recuid FROM mytable);
$insert = $this->_pdo->prepare($sql);
$insert->execute(array(
    ':hash' => $this->_getHash,
    ':tablename' => $this->_catNewTable,
    ':recuid' => $result->uid
));
  • this->_pdo连接到工作正常的数据库
  • this->_getHash生成随机哈希码
  • recuid是数字
  • tablename是一个字符串

var_dump结果:

string(185) "INSERT INTO sys_refindex SET hash=:hash, tablename=:tablename, recuid=:recuid WHERE recuid NOT IN (SELECT recuid FROM sys_refindex)" 

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE recuid NOT IN (SELECT recuid FROM sys_refindex)' at line 1SQLSTATE[42000]

如果认为WHERE语句与占位符结合存在问题?

0 个答案:

没有答案