Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

MySQL: เรียงลำดับหลายคอลัมน์พร้อมกันด้วย ORDER BY

การเรียงลำดับหลายคอลัมน์ด้วย ORDER BY คอลัมน์1, คอลัมน์2

MySQL รองรับการเรียงลำดับหลายระดับ โดยระบุคอลัมน์ใน ORDER BY คั่นด้วยเครื่องหมายจุลภาค (,) คอลัมน์แรกเป็นหลัก คอลัมน์ถัดไปใช้เรียงภายในกลุ่มที่ค่าคอลัมน์แรกเท่ากัน

ไวยากรณ์ (Syntax)

SELECT col1, col2, col3 FROM yourTableName
ORDER BY col2, col3;

ตัวอย่างการใช้งาน

สร้างตารางนักเรียนมีประเทศ:

mysql> CREATE TABLE doubleSortDemo (
    -> StudentId INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    -> StudentName VARCHAR(100),
    -> StudentCountryName VARCHAR(10)
    -> );
Query OK, 0 rows affected (0.71 sec)

mysql> INSERT INTO doubleSortDemo(StudentName, StudentCountryName) VALUES('John', 'AUS');
Query OK, 1 row affected (0.21 sec)
mysql> INSERT INTO doubleSortDemo(StudentName, StudentCountryName) VALUES('Sam', 'UK');
Query OK, 1 row affected (0.20 sec)
mysql> INSERT INTO doubleSortDemo(StudentName, StudentCountryName) VALUES('Bob', 'US');
Query OK, 1 row affected (0.16 sec)
mysql> INSERT INTO doubleSortDemo(StudentName, StudentCountryName) VALUES('Carol', 'UK');
Query OK, 1 row affected (0.32 sec)
mysql> INSERT INTO doubleSortDemo(StudentName, StudentCountryName) VALUES('David', 'AUS');
Query OK, 1 row affected (0.19 sec)
mysql> INSERT INTO doubleSortDemo(StudentName, StudentCountryName) VALUES('Larry', 'UK');
Query OK, 1 row affected (0.15 sec)

ข้อมูลก่อนเรียง:

mysql> SELECT * FROM doubleSortDemo;
+-----------+-------------+--------------------+
| StudentId | StudentName | StudentCountryName |
+-----------+-------------+--------------------+
| 1         | John        | AUS                |
| 2         | Sam         | UK                 |
| 3         | Bob         | US                 |
| 4         | Carol       | UK                 |
| 5         | David       | AUS                |
| 6         | Larry       | UK                 |
+-----------+-------------+--------------------+
6 rows in set (0.00 sec)

เรียงตามประเทศ (AUS, UK, US) แล้วตามชื่อในแต่ละประเทศ:

mysql> SELECT StudentId, StudentName, StudentCountryName
    -> FROM doubleSortDemo
    -> ORDER BY StudentCountryName, StudentName;

ผลลัพธ์:

+-----------+-------------+--------------------+
| StudentId | StudentName | StudentCountryName |
+-----------+-------------+--------------------+
| 5         | David       | AUS                |
| 1         | John        | AUS                |
| 4         | Carol       | UK                 |
| 6         | Larry       | UK                 |
| 2         | Sam         | UK                 |
| 3         | Bob         | US                 |
+-----------+-------------+--------------------+
6 rows in set (0.00 sec)

คำอธิบาย

  • เรียง StudentCountryName ก่อน (AUS < UK < US ตามตัวอักษร)
  • ในแต่ละประเทศ เรียง StudentName ตามลำดับตัวอักษร (David < John, Carol < Larry < Sam)
  • สามารถระบุ ASC/DESC แยกแต่ละคอลัมน์ได้: ORDER BY country ASC, name DESC

เคล็ดลับประสิทธิภาพ

สร้าง Index รองรับ: CREATE INDEX idx_country_name ON doubleSortDemo(StudentCountryName, StudentName); จะทำให้ ORDER BY ใช้ Index ได้ (Index Sort) ไม่ต้อง Filesort