百搜论坛欢迎您的加入!
adimg adimg
 
昨日:篇  今日:篇   总帖:篇   会员:
博主最大Lv63   
thinkphp5中常用数据库查询语句介绍     

tp_data 数据表

1.jpg

value()

1

2

3

4

5

6

7

8

9

10

11

12

$name = Db::name('data')

        -> where('id', 16)

        -> value('name');

print_r($name);

 

// 获取 tp_data 数据表中 id = 16,name 字段的值,并打印

// 结果:1111

 

/** 原生sql语句

>Prepare SELECT `name` FROM `tp_data` WHERE `id` = ? LIMIT 1

>Execute SELECT `name` FROM `tp_data` WHERE `id` = 16 LIMIT 1

*/

column()

获取一列满足条件的数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

$list = Db::name('data)

        -> where('status', 1)

        -> column('name');

print_r($list);

 

// 从 tp_data 数据表获取一列 status = 1 的 name 字段值

/** 结果:

Array(

  [0] => thinkphp

  [1] => thinkphp

  [2] => thinkphp

  [3] => thinkphp

  [4] => 7777777777

  [5] => thinkphp

  [6] => thinkphp

  [7] => thinkphp

  [8] => thinkphp

)

*/

获取一列满足条件的数据,并以id值为键名

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

$list = Db::name('data)

        -> where('status', 1)

        -> column('name', 'id');

print_r($list);

 

// 从 tp_data 数据表获取一列 status=1 的 name 字段值集合

/** 结果:

Array(

  [3]  => thinkphp

  [4]  => thinkphp

  [5]  => thinkphp

  [6]  => thinkphp

  [7]  => 7777777777

  [8]  => thinkphp

  [9]  => thinkphp

  [10] => thinkphp

  [11] => thinkphp

)

*/

获取以id为键名的数据集

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

$list = Db::name('data')

    -> where('status', 1)

    -> column('*', 'id');

print_r($list);

 

// 从 tp_data 数据表获取一列 status=1 的数据集

/** 结果:

Array(

  [3] => Array(

        [id] => 3

        [name] => thinkphp

        [status] => 1

      )

  [4] => Array(

        [id] => 4

        [name] => thinkphp

        [status] => 1

      )

  [5] => Array(

        [id] => 5

        [name] => thinkphp

        [status] => 1

      )

  ...

)

*/

聚合查询

count

max

min

avg

sum

统计 data 表的数据

1

2

3

4

5

6

$count = Db::name('data')

        -> where('status', 1)

        -> count();

echo $count;

 

// 结果:9

统计 data 表的最大 id

1

2

3

4

5

$max = Db::name('data')

        -> where('status', 1)

        -> max('id);

echo $max;

// 结果:11

简单查询

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

$result = Db::name('data')

        -> where("id > :id and name like :name",

            [

                'id' => 10,

                'name' => "%php%"

            ])

        -> select();

print_r($result);

 

/** 结果:

Array(

  [0] => Array(

          [id] => 11

          [name] => thinkphp

          [status] => 1

      )

)

*/

 

/** 原生sql语句:

>Prepare SELECT * FROM `tp_data` WHERE (id > ? and name like ?)

>Execute SELECT * FROM `tp_data` WHERE (id > '10' and name like '%php%')

*/

日期查询

日期类型int,时间戳格式

查询时间大于 2016-1-1 的数据

1

2

3

4

5

6

7

8

9

$result = Db::name('users')

        -> whereTime('reg_time', '>', '2016-1-1')

        -> select();

print_r($result);

 

/** 原生sql语句:

>Prepare SELECT * FROM `tp_users` WHERE `reg_time` > ?

>Execute SELECT * FROM `tp_users` WHERE `reg_time` > 1451577600

*/

查询本周

1

2

3

4

5

6

$result = Db::name('users')

        -> whereTime('reg_time', '>', 'this week')

        -> select();

print_r($result);

 

// 从本周星期一开始

查询最近两天添加的数据

1

2

3

4

$result = Db::name('users')

        -> whereTime('reg_time', '>', '-2 days')

        -> select();

print_r($result);

查询创建时间在 2016-1-1 ~ 2017-7-1 的数据

1

2

3

4

5

6

7

8

9

$result = Db::name('users')

        -> whereTime('reg_time', 'between', ['2016-1-1', '2017-7-1'])

        -> select();

print_r($result);

 

/** 原生sql语句:

>Prepare SELECT * FROM `tp_users` WHERE `reg_time` BETWEEN ? AND ?

>Execute SELECT * FROM `tp_users` WHERE `reg_time` BETWEEN 1451577600 AND 1483200000

*/

查询今天的数据

昨天:yesterday

本周:week

上周:last week

1

2

3

4

$result = Db::name('users')

        -> whereTime('reg_time', 'today')

        -> select();

print_r($result);

分块查询

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

Db::name('data')

    -> where('status', '>', 0)

    -> chunk(2, function($list) {

        foreach($list as $data) {

            //处理2条记录

        }

    });

 

/** 原生sql语句:

>Prepare SELECT * FROM `tp_data` WHERE `status` > ? ORDER BY `id` asc LIMIT 2

>Execute SELECT * FROM `tp_data` WHERE `status` > 0 ORDER BY `id` asc LIMIT 2

>Close stmt

>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2

>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 4 ORDER BY `id` asc LIMIT 2

>Close stmt

>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2

>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 6 ORDER BY `id` asc LIMIT 2

>Close stmt

...

>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2

>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 16 ORDER BY `id` asc LIMIT 2

>Close stmt

>Prepare SELECT * FROM `tp_data` WHERE `status` > ? AND `id` > ? ORDER BY `id` asc LIMIT 2

>Execute SELECT * FROM `tp_data` WHERE `status` > 0 AND `id` > 17 ORDER BY `id` asc LIMIT 2

>Close stmt

*/

改进

1

2

3

4

5

6

$p = 0;

do {

  $result = Db::name('data') -> limit($p, 2) -> select();

  $p += 2;

  //处理数据

} while(count($result) > 0);

推荐教程:《TP5

以上就是thinkphp5中常用数据库查询语句介绍的详细内容

 0  已被阅读了1091次  楼主 2020-06-23 13:02:22
回复列表

回复:thinkphp5中常用数据库查询语句介绍

联系站长 友链申请桂ICP备19000949号-1     桂ICP备19000949号-1
您的IP:3.17.128.129,2024-05-03 11:48:55,Processed in 0.03996 second(s).
免责声明: 本网不承担任何由内容提供商提供的信息所引起的争议和法律责任。
Powered by HadSky 7.12.9
已有0次打赏
(0) 分享
分享
取消
免责声明
1、本站资源,均来自网络,版权归原作者,所有资源和文章仅限用于学习和研究目的 。
2、不得用于商业或非法用途,否则,一切责任由该用户承担 !
如果觉得本文还不错请点个赞或者打赏点轻币哦~
拒绝伸手党,拿走请回复,尊重楼主,尊重你我他~

侵权删除请致信 E-Mail:207882320@qq.com