thinkphp中的查询语句

<?php
namespace Admin\Controller;
use Think\Controller;
class MainController extends Controller
{
    public function showList()
    {
        echo "大苹果商城";
    }
    
    public function test()
    {
        //数据访问
        //造模型对象
        //$nation = D("Nation");//连接数据库中的Nation表
        
        //查询
        //$a = $nation->select();//查询所有,返回关联数组,也是二维数组。
        //$a = $nation->select("n001,n002,n003");//通过主键值查,注意写法。
        
        //$a = $nation->find();//查一条数据,返回一维数组。
        //$a = $nation->find("n001");//按照条件查询
        
        //$a = $nation->where("name='汉族'or name='回族'")->select();//where()只能写查询条件。select才会输出,select返回的是二维数组。这种操作称为连贯操作
        
        //$a = $nation->table("info")->select();//table切换其它表格查询表格信息。上面虽然是链接的Nation表,这里切换到info表格。
        
        //$a = $nation->field("code")->select();//查询指定字段,这里查询的是code列,多个字段中间用,分隔。
        
        //$a = $nation->order("code desc")->select();//code按照降序排列
        
        //$a = $nation->limit(3)->select();//查询前3条数据,limit分页查询。
        //$a = $nation->limit(3,4)->select();//跳过3条显示4条
        
        //$a = $nation->page(2,3)->select();//page中第一个参数是第几页,第二个参数是几条数据。这里是取第2页的3条数据。
        
        //$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->select();//分组查询
        
        //$a = $nation->table("car")->field("Brand,avg(price)")->group("brand")->having("avg(price)>50")->select();//having条件查询
        
        //$a = $nation->field("Info.code,Info.name as 'name',nation.name as '民族'")->join("Info on Nation.code=Info.Nation")->select();//用join连接的时候前面的field的列要加上别名,不然会出问题。
        
        //$a = $nation->table("car")->distinct(true)->field("brand")->select();//distinct去重
        
        //$a = $nation->where("code='n003'")->getField("name");//只能写列名,获取某一列的值。
        
        //$a = $nation->count();//也可以放在连贯操作的最后用,求出数据量。
        
        //$a = $nation->table("car")->max("price");
        
        //使用原生的查询方法
        //$sql = "select * from nation";
        //$a = $nation->query($sql);//查询语句还是按照原来的调用方法。
        
        //$sql = "update nation set name='矮人族' where code='n001'";
        //$a = $nation->execute($sql);//增、删、改语句用execute调用。
        
        //var_dump($a);
    }
}

 

posted @ 2016-12-22 11:11  Strive-count  阅读(663)  评论(0编辑  收藏  举报