MySql----多条件查询

CI model里面一个多重条件搜索,
place type thefind 三个条件初始值为空(搜索all),在根据前端信息设定条件

根据条件搜出进行模糊查询
place=plcae
type=type
descrption,lostname,type=thfind
数据库 lost_message表中按照从id=$limst 开始id前9位排序的数据并根据lost_message->user_id =user_message->id
寻找第二个表user_message=>id并列进行输出

public function m_ninepic($limst,$type,$place,$thefind){
    // $sql="select fname,id from lost_message order by id desc limit 0,9";
    if($type==""&&$place==""&&$thefind=="")
    {$sql="select lost_message.fname,lost_message.lostname,lost_message.id,lost_message.place,lost_message.time ,user_message.name from lost_message inner join user_message on lost_message.user_id=user_message.id order by lost_message.id desc limit ".$limst.",9";}
else 
{
    $sql2="";
    if($type!="")
    {
        $sql2=$sql2."and lost_message.type='".$type."'";
    }
    if($place!="")
    {
        $sql2=$sql2."and lost_message.place='".$place."'";
    }
    if($thefind!="")
    {
        $sql2=$sql2."and (lost_message.type like '%".$thefind."%' or lost_message.description like '%".$thefind."%' or lost_message.lostname like '%".$thefind."%')";


    }
    $sql="select lost_message.fname,lost_message.lostname,lost_message.id,lost_message.place,lost_message.time ,user_message.name from lost_message inner join user_message on lost_message.user_id=user_message.id ".$sql2." order by lost_message.id desc limit ".$limst.",9";
}
    $data=$this->db->query($sql);
    $db=$data->result_array($data);
    return $db;
}

lost_message
这里写图片描述
user_message
这里写图片描述


版权声明:本文为Ink_cherry原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。