朱纯树博客
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语句

评论 653

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

    You actually suggested it very well.
    online pharmacy ambien canadian pharmacy east bay largo fl safe online pharmacy canada

    JosephFup15分钟前回复
  2. #659

    Appreciate it, Plenty of write ups.
    reliable canadian pharmacy best online pharmacy no prescription how to find a legitimate canadian pharmacy

  3. #658

    canadian pharmacy viagra pharmacy com canada

  4. #657

    https://canadiandrugpharmacy.best/# legit canadian online pharmacy

    Camerondyeli14小时前回复
  5. #656

    https://sildenafil100mg.store/# sildenafil 100mg tablets in india

    Jamesjoing1天前回复
  6. #655

    https://canadiandrugpharmacy.best/# onlinecanadianpharmacy

    Camerondyeli2天前回复
  7. #654

    https://metformin.pro/# metformin 1 000mg

    Jeffreydruby2天前回复
  8. #653

    prednisone 1 mg tablet order prednisone online canada

    RickyGrOfe2天前回复