我正在使用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个查询:一个检查存在问题的行……然后执行更新查询是否存在;否则为插入查询。

GOAL:替换语句,该语句接受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查询来实现。如果不满足条件,将插入新行,否则将更新匹配的字段。

09-30 15:22
查看更多