CodeIgniter之Model中的查询语句

方式一:使用$this->db->query('sql查询语句');

示例1单表查询

<!-- lang: php -->
 function selectUserByName($name){
<!-- lang: php -->
        $query = $this->db->query('select id,qq,email,address from user where name = "'.$name.'"');
<!-- lang: php -->
        return $query->result_array();
<!-- lang: php -->
}

示例2单表多条件查询:

<!-- lang: php -->
function selectInfo($productId,$userid){
<!-- lang: php -->
    $query = $this->db->query('select * from table1 where id !='.$productId.'and userid ='.$userid.' and isrecommand = 1'); 
<!-- lang: php -->
    return $quey->result_array();
<!-- lang: php -->
}

示例3按照日期排序,最新的排在前面:

<!-- lang: php -->
public function selectInfo($var1,$var2){
<!-- lang: php -->

<!-- lang: php -->
    $query = $this->db->query('select id,col2,col3,last_update_date from table1 where col4 ='.$var1.'and col5 ='.$var2.'and col6 !="" order by last_update_date DESC' );
<!-- lang: php -->
    
<!-- lang: php -->
    return $query->result_array();
<!-- lang: php -->
}

示例4联合查询:

<!-- lang: php -->
    function selectInfo($productId){
<!-- lang: php -->
    $query = $this->db->query('select * from table1 join ptable2 on table2.productextid=table1.id where table2.productid='.$productId);
<!-- lang: php -->
    return $query->result_array();
<!-- lang: php -->
}

方式二:使用CodeIgniter提供的语句

$this->db->select('要查询的字段名1[,要查询的字段名2[,要查询的字段名3[,……]]]');

$this->db->where("字段名",该字段的值);//该语句可以出现多次

$this->db->get("表名");//并返回查询的结果,通常用变量$query来接收

示例5:

<!-- lang: php -->
public function getInfo($var1){
<!-- lang: php -->
    $this->db->select("col1");
<!-- lang: php -->
    $this->db->where("col2",$var1);
<!-- lang: php -->
    $query = $this->db->get("table1");
<!-- lang: php -->
    retrun $query->result_array();
<!-- lang: php -->
}

该种方式的详细语句参见:http://codeigniter.org.cn/user_guide/database/active_record.html

转载于:https://my.oschina.net/syc2013/blog/130791

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值