博客列表 >laravel数据库链式查询及模型 - - PHP中文网线上班0604

laravel数据库链式查询及模型 - - PHP中文网线上班0604

高的PHP十期培训学习笔记
高的PHP十期培训学习笔记原创
2020年06月05日 16:57:051206浏览

laravel数据库链式查询

  1. //高级数据库链式调用查询方式
  2. public function finds()
  3. {
  4. $res = DB::table('article')->where('id', 2)->first();
  5. echo '<pre>';
  6. print_r($res);
  7. }
  8. //多条查询
  9. public function list()
  10. {
  11. // 返回的是框架自带的集合对象数据
  12. // 返回全部数据
  13. $res = DB::table('article')->get();
  14. //返回指定字段内容
  15. $res = DB::table('article')->select('cate_id', 'title')->get();
  16. //给返回的字段名称起个别名
  17. $res = DB::table('article')->select('cate_id as cid', 'title')->get();
  18. //添加查询条件 栏目ID=5
  19. $res = DB::table('article')->select('cate_id as cid', 'title')->where('cate_id', 5)->get();
  20. //添加查询条件 栏目ID<5 带运算符的查询会影响MYSQL的速度,大数据并发时容易死机
  21. $res = DB::table('article')->select('cate_id as cid', 'title')->where('cate_id', '<', 5)->get();
  22. echo '<pre>';
  23. // print_r($res);
  24. // 转为数组
  25. print_r($res->all());
  26. }
  27. //查询词查询
  28. public function likes()
  29. {
  30. // 查询包含 风格 的信息 在查询词的前后加上% 但这种会影响MYSQL速度 只在查询词的后面加% 表示查询以查询词开头的信息
  31. $res = DB::table('article')->where('title', 'like', '%风格%')->get()->all();
  32. // select * from article where title like '%风格%' and cate_id=6
  33. // 增加查询条件 栏目ID=6
  34. $res = DB::table('article')->where('title', 'like', '%风格%')->where('cate_id', 6)->get()->all();
  35. //如果需要查询 栏目ID=6 或 栏目ID=1 的信息
  36. // select * from article where cate_id=6 or cate_id=1
  37. // 查看生成的SQL语句 toSql()
  38. // $res = DB::table('article')->where('cate_id',6)->orWhere('cate_id',1)->toSql();
  39. $res = DB::table('article')->where('cate_id', 6)->orWhere('cate_id', 1)->get()->all();
  40. echo '<pre>';
  41. print_r($res);
  42. }
  43. //where in 查询 重点
  44. public function wherein()
  45. {
  46. // select * from article where id in (1,2,5)
  47. $res = DB::table('article')->whereIn('id', [1, 2, 5])->get()->all();
  48. echo '<pre>';
  49. print_r($res);
  50. }
  51. //连表查询
  52. public function joins()
  53. {
  54. //查询文章表中的作者ID和用户表中的用户ID相同的
  55. // $res = DB::table('article')->join('users', 'users.id', '=', 'article.uid')->get()->all();
  56. // 上面的语句文章的ID会被覆盖 ,所以要给他添加条件
  57. $res = DB::table('article')->join('users', 'users.id', '=', 'article.uid')->select('article.id', 'article.cate_id', 'article.title', 'users.username as nickname')->get()->all();
  58. // echo '<pre>';
  59. // print_r($res);
  60. // 将数据在视图中渲染显示
  61. $data['list'] = $res;
  62. // echo '<pre>';
  63. // print_r($data);
  64. return view('mytest', $data);
  65. }
  66. //计算所有文章的平均浏览量
  67. public function pvs()
  68. {
  69. // 1.取出所有文章数据
  70. $res = DB::table('article')->get()->all();
  71. // 2.计算平均值
  72. $avg = 0;
  73. // 3.计算总和
  74. foreach ($res as $key => $value) {
  75. $avg += $value->pv;
  76. }
  77. // 4.计算平均
  78. $avg = $avg / count($res);
  79. // 5.取整数
  80. $avg = intval($avg);
  81. echo '<pre>';
  82. // print_r($avg);
  83. // mysql 自带聚合函数查询平均浏览量
  84. // select AVG(pv) from article
  85. $res = DB::table('article')->avg('pv');
  86. //强制转换为整数
  87. $res = (int)$res;
  88. // print_r($res);
  89. // mysql 自带聚合函数查询总浏览量
  90. $res = DB::table('article')->sum('pv');
  91. //强制转换为整数
  92. $res = (int)$res;
  93. print_r($res);
  94. echo '<hr>';
  95. // mysql 自带聚合函数查询最小浏览量 最大用max 记录数count()不用参数
  96. $res = DB::table('article')->min('pv');
  97. //强制转换为整数
  98. $res = (int)$res;
  99. print_r($res);
  100. }
  101. // 增加数据
  102. public function insert2()
  103. {
  104. //单条
  105. // $res = DB::table('article')->insert(array('uid'=>2,'cate_id'=>5,'title'=>'insert测试','pv'=>0));
  106. //一次增加多条
  107. $item = array('uid' => 2, 'cate_id' => 5, 'title' => 'insert测试', 'pv' => 0);
  108. $item2 = array('uid' => 3, 'cate_id' => 2, 'title' => '第二条insert测试', 'pv' => 10);
  109. $data[] = $item;
  110. $data[] = $item2;
  111. $res = DB::table('article')->insert($data);
  112. var_dump($res);
  113. }
  114. // 增加数据并返回主键ID
  115. public function insert3()
  116. {
  117. //单条
  118. $res = DB::table('article')->insertGetId(array('uid' => 2, 'cate_id' => 5, 'title' => 'insert测试', 'pv' => 0));
  119. var_dump($res);
  120. }
  121. // 修改数据
  122. public function update2()
  123. {
  124. // 修改单条
  125. // $res = DB::table('article')->where('id',14)->update(array('title'=>'update测试'));
  126. // 修改多条
  127. $res = DB::table('article')->whereIn('id', [15, 16])->update(array('title' => 'update测试'));
  128. var_dump($res);
  129. }
  130. // 删除数据
  131. public function delete2()
  132. {
  133. // 删除一条
  134. // $res = DB::table('article')->where('id',17)->delete();
  135. // 删除多条
  136. $res = DB::table('article')->whereIn('id', [15, 16])->delete();
  137. var_dump($res);
  138. }

模型

  1. // 模型 取出所有数据
  2. //第一种方法:需要将数据表改成复数的,但可能这个表其他地方也在用
  3. public function mymodels(Article $article){
  4. $res = $article->get()->toArray();
  5. echo '<pre>';
  6. print_r($res);
  7. }
  8. //第二种方法:在模型类中指定查询的表名
  9. }
  1. namespace App;
  2. use Illuminate\Database\Eloquent\Model;
  3. class Article extends Model
  4. {
  5. //第二种方法:在模型类中指定查询的表名
  6. //格式
  7. protected $table = 'article';
  8. }

总结

数据库链式查询:
1.带运算符的查询会影响MYSQL的速度,大数据并发时容易死机
2.在查询词的前后加上% 但这种会影响MYSQL速度 只在查询词的后面加% 表示查询以查询词开头的信息

模型: 创建方法
1.XXXX 代表控制器名称,首字母要大写
php artisan make:controller XXXX
2.XXXX 代表模型名称,首字母要大写
php artisan make:model XXXX

模型: 取出所有数据
第一种方法:需要将数据表改成复数的,但可能这个表其他地方也在用
第二种方法:在模型类中指定查询的表名,格式: protected $table = 'article';

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议