ThinkPHP框架 系统规定的方法查询数据库内容!!同时也支持原生的SQL语句!

<?php
namespace Admin\Controller;
use Think\Controller;
class MainController extends Controller{
    public function test(){
        $nation = M("nation");
        //$a = $nation->select();//查所有,返回关联数组
        //$a = $nation->field("name")->select();//field方法查询指定"字段"!
        //$a = $nation->select("n001,n002,n003");//通过主键查
        //$a = $nation->find("n001");//find方法只能按照主键查一条数据
        
        //$a = $nation->where("name='汉族' or name='回族' or name='布依族'")->select();//where("字符串条件");
        
        //$a = $nation->table("Info")->select();//table方法现在查的是nation表切换查Info表;
        
        //$a = $nation->order("code desc")->select();//order方法配合着desc降序排列,从大到小,如果没有指定desc或者asc排序规则的话,默认为asc正常排序!
        
        //$a = $nation->limit(6,3)->select();//limit方法相当于分页查询,跳过前面6个,每页显示3个就是显示7,8,9!
        
        //$a = $nation->page(3,3)->select();//page方法相也是当于分页查询,跳过前3页,每页显示3个,也是显示7,8,9!
        //注:这两个分页的差别就是limit(跳过几个或几条),page(跳过几页)
        
        //$a = $nation->table("Car")->field("Brand,avg(Price)")->group("Brand")->select();
        //group方法只有一个参数,并且只能使用字符串! group()按照哪个字段分组,比如n001好几个他们就一组 n001每个商品都有他们各自的价格,然后使用avg(求和) 
        
        //$a = $nation->table("Car")->field("Brand,avg(Price)")->group("Brand")->having("avg(Price)>50")->select();
        //和上面意思一样只是多了一个having()条件只显示大于>50以上的
        
        
        //$a = $nation->alias('a')->field("b.Code as 'code',b.Name as 'name',a.name  as '民族'")->join("Info b on a.Code=b.Nation")->select();
        //如果使用join()和field()一块使用  就在field方法里给每一个列定一个别名
          
        //$a = $nation->table("car")->distinct(true)->field("brand")->select(); 
        //distinct() 去重
        
        //$a = $nation->where("code='n003'")->getField("name");//getfield()方法,获取某个字段(列名)的值加上where条件就行
        
        //$a = $nation->table("car")->sum(Price);
        //sum求总和
        
        //$a = $nation->table("car")->count(Price);
        //count求总条数
        
        //$a = $nation->table("car")->avg(Price);
        //avg()求平均值
        
        //$a = $nation->table("car")->min(Price);
        //求最小值
        
        //$a = $nation->table("car")->max(Price);
        //求最大值
        
        
        //var_dump($a);
        
        //同时也支持原生的SQL语句
        //查询语句
        $sql = "select * from info";
        $a = $nation->query($sql);//执行查询
        
        var_dump($a);
        
        
        //修改nation某条数据
        $sql = "update nation set name='人族' where code='n001'";
        $a = $nation->execute($sql);//execute()方法执行修改
        
        var_dump($a);
    }
}

 

posted @ 2016-12-22 20:02  遇事稳坐钓鱼台  阅读(305)  评论(0编辑  收藏  举报