Get the App
SLTechnology News&Howtos  ›  Database  › 

Mysql sort query field

Shulou Source: shulou.com Published: 2022-06-01 22:00:33 10月03日 Update

Sort query

Syntax:

Select query Field 1

From table

Where [filter criteria]

Order by to sort fields 2 asc ascending, desc descending, and fields 3 asc ascending and desc descending

If you don't write, it defaults to ascending order.

Case: query employee information and require wages to be sorted from high to low

SELECT * FROM employeesORDER BY salary DESC

# case 2: query department number > = 90, employee information in ascending order by entry date

SELECT * FROM employeesWHERE department_id > = 90ORDER BY hiredate ASC

# case: sort by expression, such as showing annual salary by annual salary

SELECT salary*12* (1+IFNULL (commission_pct,0)) FROM employeesORDER BY salary*12* (1+IFNULL (commission_pct,0)) DESC

# case: alias the sort field, and then sort by alias

SELECT salary*12* (1+IFNULL (commission_pct,0)) AS Annual salary FROM employeesORDER BY Annual salary ASC

# case: displays the employee's name and salary in descending order by name length (sorted by function)

SELECT LENGTH ('aaaa'); # shows a length of 4

SELECT LENGTH (last_name) AS byte length, last_name, salaryFROM employeesORDER BY LENGTH (last_name) DESC

# case: to query employee information, it is required to sort by salary first, and then by employee number, (sort by multiple fields)

SELECT * FROM employeesORDER BY salary ASC,employee_id DESC

Tags: Sort case field query employee ascending order annual salary information salary length alias name function multiple byte date condition expression syntax department Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information MySQL macOS Shulou Technology Redmi