您的位置:首页 > 编程语言 > PHP开发

php select 语句

2016-06-29 15:50 330 查看
        $goods = D('Goods');

        

        //① where()条件限制

        $goods -> where('goods_price >1000 and goods_name like "诺%"');

        //SELECT * FROM `sw_goods` WHERE ( goods_price >1000 and goods_name like "诺%" )

        $info = $goods -> select();//之前条件会被自动清空,对后续查询没有影响

        

        //② limit([偏移量,]长度) 记录数目限制

        $goods -> limit(5);

        //SELECT * FROM `sw_goods` LIMIT 5

        $info = $goods -> select();

        

        //③ field() 限制查询字段

        $goods -> field('goods_id,goods_name');

        //SELECT `goods_id`,`goods_name` FROM `sw_goods`

        $info = $goods -> select();

        

        //④ order() 排序查询

        $goods -> order('goods_price desc');

        //SELECT * FROM `sw_goods` ORDER BY goods_price desc

        $info = $goods -> select();

        

        //⑤ group() 分组查询  ,该分组的sql语句较复杂,推荐原生sql语句执行

        //查询每个品牌的商品总数量

        //select goods_brand_id,count(*) from sw_goods group by goods_brand_id

        $goods -> group('goods_brand_id');

        $goods -> field('goods_brand_id,count(*)');

        $info = $goods -> select();

        

        //$info = $goods -> group()->field()->select();

        

        //⑥ having() 设置条件

        $goods -> having('goods_price>2000');

        $info = $goods -> select();

        

        $this -> assign('info',$info);

        $this -> display();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: