Php 在yii中的mysql中获得一个maxid和condition的记录,但有时它会获得第二个记录?

Php 在yii中的mysql中获得一个maxid和condition的记录,但有时它会获得第二个记录?,php,mysql,yii,Php,Mysql,Yii,我的桌子: PHP代码: 我认为,问题在于冲突。这意味着当我得到记录时有max_id,然后在我插入新记录之前,让另一个进程插入新记录。 如果发生这种情况,那么如何处理以解决上述问题?我的解决方案: 我将表锁定,不在此表中保留任何其他会话工作。第二条记录是什么意思?按ID返回第二条记录或返回两条记录?例如:表中的Maxid是53,但有时它会得到ID=52。并且$ID_kho和$ID_san_pham是否只适合53?但为什么不实际使用yii模型来处理这些内容呢?这可能对解决这个问题没有帮助,但在ge

我的桌子:

PHP代码:


我认为,问题在于冲突。这意味着当我得到记录时有max_id,然后在我插入新记录之前,让另一个进程插入新记录。 如果发生这种情况,那么如何处理以解决上述问题?

我的解决方案:


我将表锁定,不在此表中保留任何其他会话工作。

第二条记录是什么意思?按ID返回第二条记录或返回两条记录?例如:表中的Maxid是53,但有时它会得到ID=52。并且$ID_kho和$ID_san_pham是否只适合53?但为什么不实际使用yii模型来处理这些内容呢?这可能对解决这个问题没有帮助,但在geenral,最好使用modesl,这样也可以避免语法错误。如果有任何方法可以做到这一点,请告诉我!使用select maxid是错误的,我认为唯一的查询是:==>$sql=从id为的_kho chi_tie_中选择so_luong_ton、kho_luong_ton、kho du_tru。而id_san_pham=.$id_san_pham。按id描述的订单限制1;您也可以尝试这样做,也可以使用bind参数并将where子句放入createCommand中$query=Yii::app->db->createCommandSELECT tableColumn按tableColumn DESC LIMIT 1从tableName顺序中选择tableColumn->其中'cond1=:cond1',array':cond1'=>$cond1->queryRow;以上链接仅用于了解性能。@chomiendon.com。。我不明白,你能不能用简单的方式详细地告诉我。我想,问题是冲突。这意味着当我得到记录时有max_id,然后在我插入新记录之前,让另一个进程插入新记录。如果发生此类情况,那么如何处理以解决上述问题?@chomiendon.com您能否告诉我冲突是如何发生的,并展示一些代码,如果是,我们将在理解的基础上给出解决方案。
CREATE TABLE IF NOT EXISTS `the_kho_chi_tiet_with_id` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ngay_thang` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `ma_phieu` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `id_san_pham` int(11) NOT NULL,
  `id_kho` int(11) NOT NULL,
  `khoi_luong_nhap` double NOT NULL,
  `so_luong_nhap` int(11) NOT NULL,
  `khoi_luong_xuat` double NOT NULL,
  `so_luong_xuat` int(11) NOT NULL,
  `khoi_luong_ton` double NOT NULL,
  `so_luong_ton` int(11) NOT NULL,
  `kho_du_tru` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
)
$sql = "SELECT so_luong_ton, khoi_luong_ton, kho_du_tru FROM the_kho_chi_tiet_with_id WHERE id_kho = $id_kho and id_san_pham = $id_san_pham ORDER by id DESC LIMIT 1";
$command=$connection->createCommand($sql);
$dataReader=$command->queryAll();
if($dataReader!=null)
{
    foreach($dataReader as $row)
    {
       .................
    }
}

**Get a record with maxid and condition in mysql in yii, *but sometime it get second record* !?**
  Please check with the below, hope it works!..if not please tell...

  $id_kho=373;//sample value declaration
  $id_san_pham=1;//sample value declaration

  $select="select max(id) as id,so_luong_ton, khoi_luong_ton, kho_du_tru from  
               the_kho_chi_tiet_with_id where users_ref_id=".$id_kho." and   
          status=".$id_san_pham;
  $command = Yii::app()->db->createCommand($select)->queryRow(); 
  $Maxid=$command['id'];
  $so_luong_ton=$command['so_luong_ton'];
  $khoi_luong_ton=$command['khoi_luong_ton'];
  $kho_du_tru=$command['kho_du_tru'];
$lock = $connection->createCommand('LOCK TABLES `the_kho_chi_tiet_with_id` READ');
$lock->execute();
$sql = "SELECT so_luong_ton, khoi_luong_ton, kho_du_tru FROM the_kho_chi_tiet_with_id WHERE id_kho = $id_kho and id_san_pham = $id_san_pham ORDER by id DESC LIMIT 1";
$command=$connection->createCommand($sql);
$dataReader=$command->queryAll();
if($dataReader!=null)
{
    foreach($dataReader as $row)
    {
       .................
    }
}
//Insert new record here
TheKhoChiTietWithId::model()->InsertNewRecord(1,300,1,333,1);

$unlock = $connection->createCommand('UNLOCK TABLES');
$unlock->execute();