最近在在Thinkphp中开发项目,想要使用原生的MySQL语句,最后尝试了一下,感觉对应复杂的查询更新操作,特别有帮助!下面就详细介绍下!
1、使用原生mysql核心
$Model = M();
$result = $Model->query($sql);//查询
$Model->execute($sql_ex);//更新修改删除
2、具体用法
<?php
/**
* 一亩地
*/
public function dealYimudi(){
$sql = "SELECT * from yimudi y LEFT JOIN yimudi_use yu on y.yimudi_id =yu.yimudi_id WHERE yu.yimudi_id = 12 and yu.use_status in (1,2) and y.use_end_time+86399 < UNIX_TIMESTAMP(now())";
$Model = M();
$result = $Model->query($sql);
$yimudi_ids =[];
foreach ($result as $key => $value) {
$yimudi_ids[] =$value['id'];
}
foreach ($result as $key1 => $value1) {
if(in_array($value1['id'],$yimudi_ids)){
$str_yimudi_id = $value1['id'];
$sql_ex = "update yimudi_use set use_status = 6 where id = '$str_yimudi_id'";
$Model->execute($sql_ex);
}
}
// $result = $Model->query($sql);
// dump($result);die;
}
大家可以尝试用下!