Sorting with ORDER BY · การเรียงลำดับด้วย ORDER BY
Sorting the result
The rows come back in no particular order. Add ORDER BY to sort them by a column:
ASCsorts ascending (smallest first) — this is the default.DESCsorts descending (largest first).
การจัดเรียงผลลัพธ์
แถวที่ถูกคืนมาอาจไม่เรียงลำดับเพิ่ม ORDER BY เพื่อจัดเรียงตามคอลัมน์:
SELECT name, score FROM student ORDER BY score DESC;
ASCเรียง จากน้อยไปมาก (เล็กสุดก่อน) — นี่คือค่าเริ่มต้นDESCเรียง จากมากไปน้อย (ใหญ่สุดก่อน)
Sorting by more than one column
Sort text columns alphabetically:
You can sort by several columns: ORDER BY form ASC, score DESC sorts by form first, then by score within each form. ORDER BY always comes last in the query.
การจัดเรียงตามคอลัมน์มากกว่าหนึ่งคอลัมน์
เรียงคอลัมน์ข้อความตามตัวอักษร:
SELECT name FROM student ORDER BY name;
คุณสามารถเรียงตามหลายคอลัมน์: ORDER BY form ASC, score DESC จะเรียงตามชั้นเรียนก่อน แล้วเรียงตามคะแนน ภายใน แต่ละชั้นเรียน ORDER BY จะอยู่ท้ายสุดของคำสั่งเสมอ
Common mistakes
- The default sort is ascending; add
DESCfor descending. ORDER BYcomes near the end of the query.
ข้อผิดพลาดทั่วไป
- การจัดเรียงเริ่มต้นคือแบบจากน้อยไปมาก; เพิ่ม
DESCสำหรับแบบจากมากไปน้อย ORDER BYอยู่ใกล้ปลายของคำสั่ง
ORDER BY sorts the rows · ORDER BY จะเรียงแถวข้อมูล
ORDER BY puts the result rows in order, like a sort. · ORDER BY จะจัดลำดับแถวผลลัพธ์ออกตามระเบียบ เหมือนกับการเรียงลำดับ
Show every student's name and score, highest score first. · แสดง name และ score ของนักเรียนทุกคน เรียงจากคะแนนสูงสุดเป็นอันดับแรก
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Show every student's name, sorted alphabetically (A to Z). · แสดง name ของนักเรียนทุกคน โดยเรียง ตามตัวอักษร (A ถึง Z)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่