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

วิธีจัดเรียงหลายคอลัมน์ในคำสั่ง Query เดียวของ MySQL


ระบุหลายคอลัมน์ใน ORDER BY คั่นด้วยคอมมา:

ไวยากรณ์

Select Col1,Col2,... from table_name ORDER BY Col1, Col2,...

ตัวอย่าง

จัดเรียงตาราง Student ตาม Name และ RollNo:

mysql> Select Name, RollNo from student order by name,rollno;
+--------+--------+
| name   | rollno |
+--------+--------+
| Aarav  |    150 |
| Aryan  |    165 |
| Gaurav |    100 |
+--------+--------+
3 rows in set (0.00 sec)

หรือเลือกทุกคอลัมน์:

mysql> Select * from student order by name,rollno;
+--------+--------+--------+
| Name   | RollNo | Grade  |
+--------+--------+--------+
| Aarav  |    150 | M.SC   |
| Aryan  |    165 | M.tech |
| Gaurav |    100 | B.tech |
+--------+--------+--------+
3 rows in set (0.00 sec)