回覆列表
  • 1 # 牙醬
    1、直接使用資料庫執行原生SQL操作

    Db::query("select * from xx_user where id=?",[1]);

    Db::execute("insert into xx_user (id, name) values (?, ?)",[1,"Kate"]);

    也支援命名佔位符繫結

    Db::query("select * from xx_user where id=:id",["id"=>1]);

    Db::execute("insert into xx_user (id, name) values (:id, :name)",["id"=>1,"name"=>"Kate"]);

    2、查詢構造器查詢

    查詢一個數據

    Db::table("xx_user")->where("id",1)->find();

    查詢資料集

    Db::table("xx_user")->where("status",1)->select();

    閉包函式查詢

    Db::select(function($query){

    $query->table("xx_user")->where("status",1);

    });

    複雜條件的查詢

    $result = Db::table("xx_user")->where(function ($query) {

    $query->where("id", 1)->whereor("id", 2);

    })->whereOr(function ($query) {

    $query->where("name", "like", "Kate")->whereOr("name", "like", "Mary");

    })->select();

    3、透過模型查詢

    獲取單個數據的方法

    $user = User::get(1);或者

    $user = new User();

    $user->where("name", "Kate")->find();

    查詢資料集

    $user = new User();

    $user->where("name", "like","%xx%")

    ->limit(10)

    ->order("id", "desc")

    ->select();

  • 中秋節和大豐收的關聯?
  • 統計師報考條件?