我正在使用 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/