删除数据.delete

函数原型

删除成功后,返回影响行数,没有修改记录返回 0。

  1. public function delete($mixData = null, $arrBind = [], $bFlag = false);

用法如下

  1. # DELETE FROM `test` WHERE `test`.`id` = 1 ORDER BY `test`.`id` DESC LIMIT 1
  2. /*
  3. (
  4. )
  5. */
  6. Db::table('test')->
  7. where('id', 1)->
  8. limit(1)->
  9. orderBy('id desc')->
  10. delete();

支持 join 方式删除数据

  1. # DELETE t FROM `test` `t` INNER JOIN `hello` `h` ON `h`.`name` = '小牛' WHERE `t`.`id` = 1
  2. /*
  3. (
  4. )
  5. */
  6. Db::table('test as t')->
  7. innerJoin(['h' => 'hello'], [], 'name', '=', '小牛')->
  8. where('id', 1)->
  9. limit(1)->
  10. orderBy('id desc')->
  11. delete();

支持 using 方式删除数据

  1. # DELETE FROM t1 USING `t2`,`t3`,`test` `t1` WHERE `t1`.`id` = `t2`.`id` AND `t2`.`id` = `t3`.`id`
  2. /*
  3. (
  4. )
  5. */
  6. Db::table('test as t1')->
  7. where('t1.id', '{[t2.id]}')->
  8. where('t2.id', '{[t3.id]}')->
  9. using('t2,t3')->
  10. limit(1)->
  11. orderBy('id desc')->
  12. delete();