[Day25] Query Builder查询生产器

介绍

Laravel 的数据库查询构建器提供了一个方便、流畅的界面来创建和运行数据库查询。它可用於在您的应用程序中执行大多数数据库操作,并适用於所有支持的数据库系统。

Laravel 查询构建器使用 PDO 参数绑定来保护您的应用程序免受 SQL 注入攻击。无需清理作为绑定传递的字符串。


接续昨天,先在products的资料表新增了几笔测试资料。

https://ithelp.ithome.com.tw/upload/images/20210923/20128999pbJqogtPIs.jpg

get all row

跟昨天一样使用php artisan tinker来操作
在tinker中输入DB::table('products')->get();他将会捞取所有products table的资料

>>> DB::table('products')->get()
=> Illuminate\Support\Collection {#3504
     all: [
       {#3499
         +"id": 1,
         +"code": "1",
         +"name": "iphone",
         +"price": 100,
         +"quantity": 100,
         +"created_at": null,
         +"updated_at": null,
       },
       {#3505
         +"id": 2,
         +"code": "2",
         +"name": "computer",
         +"price": 1000,
         +"quantity": 100,
         +"created_at": null,
         +"updated_at": null,
       },
       {#3503
         +"id": 3,
         +"code": "3",
         +"name": "gamecard",
         +"price": 50,
         +"quantity": 100,
         +"created_at": null,
         +"updated_at": null,
       },
       {#3502
         +"id": 4,
         +"code": "4",
         +"name": "EDIFIER",
         +"price": 10000,
         +"quantity": 100,
         +"created_at": null,
         +"updated_at": null,
       },
     ],
   }

where

可以来寻找价钱是1000元的商品名称:

>>> DB::table('products')->where('price', '=',1000)->value('name');
=> "computer"

pluck

可以使用该pluck方法,将会返回自订义键值:

>>> DB::table('products')->pluck('name','id')
=> Illuminate\Support\Collection {#3523
     all: [
       1 => "iphone",
       2 => "computer",
       3 => "gamecard",
       4 => "EDIFIER",
     ],
   }
>>>

Aggregates

找到商品数量:

>>> DB::table('products')->count();
=> 4

找到价格最贵商品:

>>> DB::table('products')->max('price');
=> 10000

select

如果不希望查询到的是所有的列,那就可以使用select语句,自订义回传栏位

查询所有商品及价格:

>>> DB::table('products')->select('name', 'quantity')->get();
=> Illuminate\Support\Collection {#3520
     all: [
       {#3522
         +"name": "iphone",
         +"quantity": 100,
       },
       {#3512
         +"name": "computer",
         +"quantity": 100,
       },
       {#3525
         +"name": "gamecard",
         +"quantity": 100,
       },
       {#3518
         +"name": "EDIFIER",
         +"quantity": 100,
       },
     ],
   }

先在资料库加上一笔商品名称重复资料
https://ithelp.ithome.com.tw/upload/images/20210923/20128999uClyF5vILe.jpg
找到不重复的商品名称:

>>> $users = DB::table('products')->select('name')->distinct()->get();
=> Illuminate\Support\Collection {#3510
     all: [
       {#3489
         +"name": "iphone",
       },
       {#3511
         +"name": "computer",
       },
       {#3507
         +"name": "gamecard",
       },
       {#3515
         +"name": "EDIFIER",
       },
     ],
   }
>>>

明天来写Query Builder CRUD


<<:  不容小觑的数据分析工具 - Excel:基础函数介绍

>>:  Python - 根据输入的英文字母排列出有意义的单词-参考笔记

C# 入门之字符串处理

在很多情况下,我们需要通过程序去处理一些文本,文本都是以字符串表示的,所以我们今天来看一看,使用 C...

Day2 参加职训(机器学习与资料分析工程师培训班),记录学习内容(6/30-8/20)

人工智慧与资料分析专题 今天课程主要在说明专题的制作,研究过程分为4个阶段: 研究动机: 1.研究目...

Day21 ( 高级 ) 戳泡泡 ( 视讯侦测 )

戳泡泡 ( 视讯侦测 ) 教学原文参考:戳泡泡 ( 视讯侦测 ) 这篇文章会介绍,如何在 Scrat...

唤醒与生俱来的数学力 (1)

缘起 在线上课程中,偶尔会遇到有一种类型的人这样说:「我很想学,但我数学不好,所以学程序的时候不那麽...

Ubuntu巡航记(5) -- Kaldi 安装

前言 Kaldi 是自动语音辨识(Automatic Speech Recognition, ASR...