2023年6月23日发(作者:)
laravel数据查询(集合)Inner join:$arr = [];$str = 'age = ?';$arr[] = '18';$str = ' and x = ? and y = ?';$arr[] = 'x';$arr[] = 'y';$list_temp = DB::table('account_info') ->join('users','t_nu','=','account_t_nu') ->whereRaw($str, $arr) ->orderBy('user_id', 'DESC') ;$list_temp->where('created_at', '>=', $start_time);$list = $list_temp->get();//循环 $list 直接 使⽤ ->属性 获取值即可,//连表也是 所以如果有相同的字段最好命名别名。foreach($list as $v){ $user_id = $v->user_id; // 表account_info的 user_id字段 $LOGIN = $v->country; // 表users的country字段}应⽤别名:$list_temp = DB::table('account_info as ai') ->join('MT4_USERS as mu','','=','4_account') ->whereRaw($strWhere, $arrWhere) ->orderBy('_id', 'DESC') ;//或者User::from('user as u')->join('comments as c','','=','_id') // 未测试join ON 判断的同时 添加 and 过滤:
使⽤ join 然后 where , sql 类似: select * from A LEFT JOIN B ON = where > 100 AND <100;
但是有的时候我们希望sql 类似:select * from A LEFT JOIN B ON = AND <100 where > 100;
即 AND 在 ON 之后那么 代码举例:DB::table('users as u')->select('_id','')->leftJoin('class as c', function($join){ $join->on('_id', '=', '_id') ->on('', '=', '2');})->get();hasOne:
hasOne ⽤于 ⼀对⼀ 例如 user 表 hasOne phone
使⽤的时候直接 User::find(1)->phone 。 如果只是获取某个⽤户的 phone 这么⽤没问题,但是如果批量获取⽤户的phone 这样就不合适,因为每次获取都会
执⾏sql : 类似 select * from phone where user_id = $id;
所以应当使⽤ withwith(渴求式加载):$data = User::with('phone')->get();$data = User::with(['phone' => function ($query) { $query->where('title', 'like', '%first%');}])->get();使⽤ 循环 $data 如果获取phone 直接 $v->phone 即可,但是这样查询结果不能因为关联表的值影响 $data 值数量
例如 select * from user inner join phone on = _id where like ‘189%’;
因为 使⽤ with 的sql 是先 查询所有的 user 然后结果再放到 in 条件⾥ 给 phone 类似:
select * from user where XX; user_id 值为 1,2,3,4 然后再 select * from phone where user_id IN (1,2,3,4); 再将值和 前⾯的值整合。
所以想要phone 的条件影响 user 的值集合是做不到的。可以使⽤ whereHaswhereHas:$list = AccountInfo::whereHas('users',function ($query) use($start_time,$end_time){ if($start_time){ $query->where('created_at', '>=', $start_time); } if($end_time){ $query->where('created_at', '<=', $end_time); }})->whereRaw($str, $arr)->orderBy('user_id', 'DESC')->get();即 whereHas 相当于使⽤ where exists
使⽤的时候 循环 foreach($list as $v) 然后 ->AccountInfo 的字段,如果需要 users 的字段值就需要 $v->users->+users的字段,但是这⾥就会产⽣⼀个sql(⾮渴求式)
所以如果还想要 关联表的 值 那么还是使⽤ DB::table(‘a’)->join 即 inner join 好了指定查询字段://使⽤all :AccountInfo::all(['user_id','user_name']);//使⽤get:AccountInfo::where('user_id','<','10')->get(['user_id','user_name']);//使⽤selectAccountInfo::where('user_id','<','20')->select(['user_id','user_name'])->get();使⽤ chunk:AccountInfo::orderBy('user_id','desc')->chunk(100,function($accounts){ foreach ($accounts as $account){ var_dump($account->toArray()); }});打印sql 类似执⾏了循环分页,⼀页 100 默认主键升序排序,可以⾃定义 orderBy. 也可以添加where 等条件判断是否有结果:
使⽤ eloquent 获取结果,不能使⽤ empty 或者 is_null 等来判断,因为如果没有结果,empty 也是 false
因为返回的空结果 也是 IlluminateDatabaseEloquentCollection 对象实例
Eloquent 已经封装了⼏个判断⽅法:$result = Model::where(...)->get();if ($result->first()) { }if (!$result->isEmpty()) { }if ($result->count()) { }laravel lockForUpdate sharedLock//考虑当有并发更新此⾏数据的时候使⽤。//insert ,delete 等sql操作不需要使⽤, 读取(select)的时候使⽤ lockForUpdate 或者 sharedLock。//举例:DB::table('user')->where('age', '>', 18)->lockForUpdate()->get();//或者User::where(xxx)->lockForUpdate()->first();//共享锁:DB::table('users')->where('votes', '>', 100)->sharedLock()->get();//lockForUpdate 相当于 sql:select * from users where id =2 for update//sharedLock 相当于 sql:select * from users where id=2 lock in share mode;注意:锁要使⽤在 事务中,查询的⾏务必使⽤索引,避免全表扫描,这样会导致锁整个表,⽽不是某⼀⾏。
使⽤锁,这个表的引擎必须是 innodb.
具体可以查看 :
orderBy 排序:$list->orderBy('id','asc');根据 id 升序排序多个字段排序则:$list->orderBy('id','asc')->orderBy('name','desc');⾃定义排序规则:(使⽤ orderByRaw)$list->orderByRaw('CONVERT(chinese_name USING gbk) ASC'); // 中⽂排序sum 多个字段或者⾃定义规则:DB::table('user')->select(['sum(age)','sum(score)'])->groupBy('user_id');
原⽣ sql select 或者其它通⽤语句:$a = DB::select("SELECT user_id,user_name FROM account_info;");var_dump($a);返回:array (size=1) 0 => object(stdClass)[293] public 'user_id' => int 3 public 'user_name' => string 'aa' (length=2)通⽤语句:(只会返回 true/false)DB::statement("set SESSION sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUT复杂 sql 举例:(left join ,⾃定义 select,使⽤DB::raw , 使⽤ IFNULL ,select ⾥⾯套 select )$list_temp = DB::table('account_info') ->leftJoin('user as us','t_nu','=','account_t_nu') ->whereRaw($str, $arr) ->orderBy('user_id', 'DESC');$count = $list_temp->count();$list = $list_temp ->select([ 'account_t_nu as account_nu', 'account_e_name as chinese_name', 'account_t_type as account_type', 'E as BALANCE', ' as CREDIT', ' as EQUITY', ' as MARGIN', 'GE as LEVERAGE', 'E as REGDATE', DB::raw('IFNULL((SELECT SUM(`trades_going_long`.`VOLUME`) from `trades` as `trades_going_long` where `trades_going_long`.`account_nu DB::raw('IFNULL((SELECT SUM(`trades_going_short`.`VOLUME`) from `trades` as `trades_going_short` where `trades_going_short`.`account_nu DB::raw('IFNULL((SELECT SUM(`trades_profit`.`PROFIT`) from `trades` as `trades_profit` where `trades_profit`.`account_nu` = `account_info ]) ->skip($start) ->take($pageSize) ->get();mysql 严格模式 only_full_group_by 等 (mysql 5.6 + 默认开启的)
有的时候报错 Syntax error or access violation 1055 ‘xxx’ isn’t in GROUP BY xxx 这种通常是因为开启了 only_full_group_by引起的
所以我们可以修改 [mysqld]sql_mode=IGNORE_SPACE,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBS然后重启 mysql 服务
/etc/init.d/mysql restart
如果还是报错: 那么可能是 laravel 框架的限制,即 修改 config/ mysql 链接下的 strict 配置,为 false
如果 为 true ,laravel 会执⾏:protected function strictMode(){} return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_B持续更新…
发布者:admin,转转请注明出处:http://www.yc00.com/web/1687517893a16396.html
评论列表(0条)