php - 在插入数据之前检查现有条目

标签 php mysql sql insert

只有当新客户不存在时,我才尝试插入新客户。我认为 SQL 语句有问题,但我也想在 PHP 中提供上下文:

   $sql = "INSERT INTO clients
        (`studentEmail`, `studentPassword`, `parentEmail`, `parentPassword`,
        `studentFirstName`, `studentLastName`, `studentPhone`, `parentFirstName`,
        `parentLastName`, `parentPhone`, `school`)
         VALUES ('$studentEmail', '$studentPassword', '$parentEmail',
        '$parentPassword', '$studentFirstName', '$studentLastName',
        '$studentPhone', '$parentFirstName', '$parentLastName', '$parentPhone', '$school')
        SELECT studentEmail
        FROM clients
        WHERE not exists (select * from clients where studentEmail == '"$studentEmail"')";

最佳答案

在插入语句中,selectvalues 是互斥的。而不是:

insert table1 (col1, col2, col3, ...)
values (1, 2, 3, ...)
select col1
where  not exists (...)

尝试:

insert table1 (col1, col2, col3, ...)
select 1, 2, 3, ...
where  not exists (...)

关于php - 在插入数据之前检查现有条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8566569/

相关文章:

php - MySQL模式,哪种方式性能更高? M-N 关系还是将所有相关 ID 保存为字符串?

mysql - 自动增量在 MySQL 中没有重置

php - 每天从 CSV 文件更新 MySQL(存储过程中不允许加载数据)

php - Doctrine2 - 从没有直接关系的多个表中选择

php - 子目录中的 Zend 应用程序使 Zend 布局链接正常工作

php - 将 IGNORE 添加到由架构更新创建的 ALTER TABLE 语句

PHP,日期函数的使用

java - 我如何使用java中的单个结果集检查是否有记录并从数据库中获取记录值?

java - 什么时候应该开始使用字符串替换而不是 sprintf?

javascript - 在纯 JavaScript 中构建具有多个步骤的表单向导的最佳实践?