朱纯树博客
VPS测评推荐网站
cloudacead cloudacead

tp5 有点技巧性的sql语句

  1. in查询,默认会将查询的条件按数字正序排序,那我们不需要排序怎么办?

    $exp = new thinkdbExpression(‘field(id,’.implode(‘,’,$province_ids).’)’);

    $list = Db::name(‘表名’)->where([‘id’=>[‘in’,$province_ids],’status’=>1])->field(‘id,name’)->order($exp)->select();

  2. 闭包查询,子查询

    $list = Db::name(‘dict_region’)->where(‘id’,’IN’,function($query) use($where){

    $query->table(‘这里的表名要带前缀’)->where($where)->field(‘province’);

    })->field(‘id,name’)->select();

  3. order排序里面使用函数

    $arr = Db::name(‘表名’)

    ->alias(‘a’)

    ->join(‘product_goods b’,’a.goods_id=b.id’)

    ->where($where)

    ->group(“a.goods_id”)

    ->orderRaw(“sum(a.publish_num) desc”)   //将a表的publish_num的和作为排序的标准

    ->field(“goods_id as id,b.name,sum(a.publish_num) as num”)

    ->select();

  4. wherOr的查询

    $list = Db::name(‘message’)

    ->alias(‘a’)

    ->join(‘user b’,’a.user_id=b.id’)

    ->where($where)

    ->where(function ($query) use ($keywords) {

    $query->whereOr([‘username’=>[‘like’,’%’.$keywords.’%’]])   //昵称模糊搜索

    ->whereOr([‘nickname’=>[‘like’,’%’.$keywords.’%’]]);    //昵称模糊搜索

    })

    ->order(‘a.id desc’)

    ->select();

文章来源于互联网:tp5 有点技巧性的sql语句

赞(0) 打赏
未经允许不得转载:VPS测评推荐网站 - 朱纯树博客 » tp5 有点技巧性的sql语句

评论 229

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #240

    mobile gambeling
    play blackjack online money
    free online casino no deposit

  2. #239

    online casino for real money
    online casinos real money
    online casino no deposit welcome bonus

  3. #238

    pain meds online without doctor prescription prescription drugs without doctor approval

    DarnellBrart2天前回复
  4. #237

    https://medrxfast.com/# canadian online drugs

    ThomasBib2天前回复