Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mysql sort query field

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report