php - 米克罗数据库 : how to perform a replace with where statement instead of primary key

标签 php mysql

我正在使用 php/mysql 库,meekrodb

如果满足 3 个字段条件,我想更新现有行;否则插入一个新行。

问题:

An "update" will not create a new row if a current one does not exist.
An "insert" will insert a redundant row even if a current row already exists.
A "replace" requires a primary key.

目前,我似乎必须执行 2 个查询:一个检查是否存在有问题的行......然后如果确实存在则执行更新查询;否则插入查询。

目标:替换语句接受where子句而不是依赖于主键...

$where = new WhereClause( 'and' ); 
$where->add( 'field_A=%s'    , 'aaaa' ) ;
$where->add( 'field_B=%s'    , 'bbbb' ) ;
$where->add( 'field_C=%s'    , 'cccc' ) ;

DB::replace( 'my_table', 
             array( 'field_A' => 'aaaa' ,
                    'field_B' => 'bbbb' ,
                    'field_C' => 'cccc' ,
                    'field_D' => 'dddd'  
                   ) ,
             $where

           );

最佳答案

您的说法是“如果当前行不存在,‘更新’不会创建新行。”不正确。

您试图实现的目标将通过 UPDATE 查询来实现。如果不满足条件,将插入新行,否则将更新匹配的字段。

关于php - 米克罗数据库 : how to perform a replace with where statement instead of primary key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21436063/

相关文章:

PHP : Keeping track of first login and last logout in mysql

php - 在PHP中还原默认错误报告

mysql - 创建一个学生不参加重复类(class)的触发器(mysql)

html - 是否可以在 mySQL/JSP 中使用 URL 参数?

MySQL INSERT IGNORE 不适用于我的 7dtd 服务器脚本

mysql - 如何删除mysql中可变数量的行?

php - 在php中使用json_encode的Mysql更新查询问题

PHP 包括。我似乎无法弄清楚

php - 如何使用mysql order by从更相似的值中仅选择一个值

javascript - 具有限制 dinamis 的 jquery 复选框