如何在 Laravel 中使用 OrderBy 对多个列进行排序?


ORDERBY 子句用于按升序或降序排列表中的列。默认情况下,它按升序排序列,如果需要按降序排序,则应与子句一起使用DSC

语法

以下是此语句的语法:

SELECT column1, column2, ... 
FROM table_name 
ORDER BY column1, column2, ... ASC|DESC;

假设我们在 MySQL 数据库中使用以下查询创建了一个名为 Students 的表:

CREATE TABLE students(
   id           INTEGER      NOT  NULL   PRIMARY   KEY,
   name         VARCHAR(10)  NOT NULL,
   email        VARCHAR(15)  NOT NULL,
   created_at   VARCHAR(27)  NOT NULL,
   updated_at   VARCHAR(27)  NOT NULL,
   address      VARCHAR(3)   NOT NULL
);

并使用以下 INSERT 语句填充它:

INSERT INTO students(id,name,email,created_at,updated_at,address,age) VALUES (1,'Siya Khan','[email protected]','2022-05-01T13:45:55.000000Z','2022-05-01T13:45:55.000000Z','Xyz', 20);

INSERT INTO students(id,name,email,created_at,updated_at,address,age) VALUES (2,'Rehan Khan','[email protected]','2022-05-01T13:49:50.000000Z','2022-05-01T13:49:50.000000Z','Xyz', 18);

INSERT INTO students(id,name,email,address,age) VALUES (3,'Rehan Khan','[email protected]','testing',20);

INSERT INTO students(id,name,email,address,age) VALUES (4,'Rehan','[email protected]','abcd',15);

INSERT INTO students(id,name,email,address,age) VALUES (5,'Nidhi Agarwal','[email protected]','abcd',20);

INSERT INTO students(id,name,email,address,age) VALUES (6,'Ashvik Khanna','[email protected]','oooo',16);

INSERT INTO students(id,name,email,address,age) VALUES (7,'Viraj Desai','[email protected]','test',18);

INSERT INTO students(id,name,email,address,age) VALUES (8,'Priya Singh','[email protected]','test123',20);

如果检索创建的表,它将如下所示:

+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+
| id |       name    |        email     |              created_at     |              updated_at     |address  | age  |
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+
|  1 |     Siya Khan | [email protected]   | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz     | 20   |
|  2 |    Rehan Khan | [email protected]  | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz     | 18   |
|  3 |    Rehan Khan | [email protected]  | NULL                        | NULL                        | testing | 20   |
|  4 |         Rehan | [email protected]  | NULL                        | NULL                        | abcd    | 15   |
|  5 | Nidhi Agarwal | [email protected]  | NULL                        | NULL                        | abcd    | 20   |
|  6 | Ashvik Khanna | [email protected] | NULL                        | NULL                        | oooo    | 16   |
|  7 |   Viraj Desai | [email protected]  | NULL                        | NULL                        | test    | 18   |
|  8 |   Priya Singh | [email protected]  | NULL                        | NULL                        | test123 | 20   |
+----+---------------+------------------+-----------------------------+-----------------------------+---------+------+
8 rows in set (0.00 sec)

在本文中,我们将使用 eloquent 模型 student,因此要使用 orderBy,语法将是:

Student::orderBy();

以下是调用多个列上的 order by 的 MySQL 查询:

SELECT * FROM 'students' ORDER BY fieldname ASC/DESC

示例 1

以下程序使用 ORDERBY 子句检索表的多个列:

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index() { echo $student = Student::orderBy('name', 'DESC') ->orderBy('email', 'ASC') ->get(); } }

输出

以上程序的输出为:

[{"id":1,"name":"Siya Khan","email":"[email protected]","address":"Xyz"},
{"id":2,"name":"Rehan Khan","email":"[email protected]","address":"Xyz"}]

使用的查询

以上程序的 select 查询为:

SELECT * FROM 'students' ORDER BY 'name' DESC, 'email' ASC

如果在 MySQL 中执行以上查询,您将获得以下输出

mysql> SELECT * FROM students ORDER BY 'name' DESC, 'email' ASC;
+----+------------+-----------------+-----------------------------+-----------------------------+---------+
| id | name       |          email  | created_at                  |        updated_at           | address |
+----+------------+-----------------+-----------------------------+-----------------------------+---------+
| 1  | Siya Khan  | [email protected]  | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz     |
| 2  | Rehan Khan | [email protected] | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz     |
+----+------------+-----------------+-----------------------------+-----------------------------+---------+
2 rows in set (0.00 sec)

示例 2

以下是如何在 Laravel 中使用 ORDERBY 子句的另一个示例。这里,在 name 和 email 上给出的orderByASC

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index() { echo $student = Student::orderBy('name', 'ASC') ->orderBy('email', 'ASC') ->get(); } }

输出

以上程序的输出为:

[{"id":2,"name":"Rehan Khan","email":"[email protected]","created_at":"2022-05-01T13:49:50.000000Z","updated_at":"2022-05-01T13:49:50.000000Z","address":"Xyz"},{"id":1,"name":"Siya Khan","email":"[email protected]","created_at":"2022-05-01T13:45:55.000000Z","updated_at":"2022-05-01T13:45:55.000000Z","address":"Xyz"}]

使用的查询

以上情况下的查询为:

SELECT * FROM 'students' ORDER BY 'name' ASC, 'email' ASC;

在 MySQL 中执行时,输出如下

mysql> SELECT * FROM students ORDER BY name ASC, email ASC;
+----+------------+-----------------+-----------------------------+-----------------------------+---------+
| id | name       | email           | created_at                  | updated_at                  | address |
+----+------------+-----------------+-----------------------------+-----------------------------+---------+
| 2  | Rehan Khan | [email protected] | 2022-05-01T13:49:50.000000Z | 2022-05-01T13:49:50.000000Z | Xyz     |
| 1  | Siya Khan  | [email protected]  | 2022-05-01T13:45:55.000000Z | 2022-05-01T13:45:55.000000Z | Xyz     |
+----+------------+-----------------+-----------------------------+-----------------------------+---------+
2 rows in set (0.00 sec)

更新于: 2022-08-29

6K+ 浏览量

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告