依崇稀

导航

 

page

// 查询第一页数据
Db::table('think_article')->limit('0,10')->select(); 
// 查询第二页数据
Db::table('think_article')->limit('10,10')->select(); 

group

Db::table('think_user')
    ->field('user_id,test_time,username,max(score)')
    ->group('user_id,test_time')
    ->select();
SELECT user_id,test_time,username,max(score) FROM think_score GROUP BY user_id,test_time

having

Db::table('think_user')
    ->field('username,max(score)')
    ->group('user_id')
    ->having('count(test_time)>3')
    ->select(); 

join

Db::table('think_artist')
->alias('a')
->join('think_work w','a.id = w.artist_id')
->join('think_card c','a.card_id = c.id')
->select();

union

NION操作用于合并两个或多个 SELECT 语句的结果集。

使用示例:

Db::field('name')
      ->table('think_user_0')
      ->union('SELECT name FROM think_user_1')
      ->union('SELECT name FROM think_user_2')
      ->select();

闭包用法:

Db::field('name')
      ->table('think_user_0')
      ->union(function($query){
      	$query->field('name')->table('think_user_1');
        })
      ->union(function($query){
      	$query->field('name')->table('think_user_2');
        })
      ->select();

或者

Db::field('name')
      ->table('think_user_0')
      ->union(['SELECT name FROM think_user_1','SELECT name FROM think_user_2'])
      ->select();

支持UNION ALL 操作,例如:

Db::field('name')
      ->table('think_user_0')
      ->union('SELECT name FROM think_user_1',true)
      ->union('SELECT name FROM think_user_2',true)
      ->select();

或者

Db::field('name')
      ->table('think_user_0')
      ->union(['SELECT name FROM think_user_1','SELECT name FROM think_user_2'],true)
      ->select();


cache

Db::table('think_user')->where('id=5')->cache(true)->find();

这里的缓存自动更新是指一旦数据更新或者删除后会自动清理缓存(下次获取的时候会自动重新缓存)。

当你删除或者更新数据的时候,可以使用cache方法手动更新(清除)缓存,例如:

Db::table('think_user')->cache('user_data')->select([1,3,5]);
Db::table('think_user')->cache('user_data')->update(['id'=>1,'name'=>'thinkphp']);
Db::table('think_user')->cache('user_data')->select([1,5]);

最后查询的数据不会受第一条查询缓存的影响,确保查询和更新或者删除使用相同的缓存标识才能自动清除缓存。

如果使用find方法并且使用主键查询的情况,不需要指定缓存标识,会自动清理缓存,例如:

Db::table('think_user')->cache(true)->find(1);
Db::table('think_user')->update(['id'=>1,'name'=>'thinkphp']);
Db::table('think_user')->cache(true)->find(1);


comment

Db::table('think_score')->comment('查询考试前十名分数')
    ->field('username,score')
    ->limit(10)
    ->order('score desc')
    ->select();

最终生成的SQL语句是:

SELECT username,score FROM think_score ORDER BY score desc LIMIT 10 /* 查询考试前十名分数 */
 
 
posted on 2020-05-08 09:12  依崇稀  阅读(129)  评论(0编辑  收藏  举报