mysql actual combat skills

1. The two fields in the mysql data table are connected to another table at the same time

For example, the user id, the inviter id and the user table need to be connected to the userinfo table to view the names corresponding to the two fields.
Method : set an alias for the table

 M('transfer_amount ta')
->join("yixiang_user u1 on u1.uid = ta.from_uid")
 ->join("yixiang_user u2 on u2.uid = ta.to_uid")
 ->join("yixiang_user_info ui1 on ui1.uid = u1.uid")
 ->join("yixiang_user_info ui2 on ui2.uid = u2.uid")
 ->find();

2. Catch the database insertion exception and then roll back the data to prevent data errors

Note: must be the innodb data engine

 M()->startTrans();
try{
    
    
  M('user')->where(array('uid'=>1))->setInc("num");  //user表存在
  M('exception')->where(array("id"=>1))->setInc("num");  //exception表不存在
  }catch (\Exception $e){
    
    
      M()->rollback();
      echo $e->getMessage();
  }
  M()->commit();

Guess you like

Origin blog.csdn.net/kevlin_V/article/details/106800152