Sunday, March 1, 2020

Order, Search, and Sort MySQL Data

Order, Search, and Sort MySQL Data When you query a MySQL database, you can sort the results by any field in an ascending or descending order just by adding ORDER BY at the end of your query. You use ORDER BY field_name ASC for an ascending sort (which is the default) or ORDER BY field_name DESC for a descending sort. You can use an ORDER BY clause in a SELECT statement, SELECT LIMIT or DELETE LIMIT statement. For example: SELECT * FROM addressORDER BY name ASC; The code above retrieves data from an address book and sorts the results by the persons name in an ascending fashion. SELECT email FROM addressORDER BY email DESC; This code selects only the email addresses  and lists them in descending order. Note: If you dont use an ASC or DESC modifier in the ORDER BY clause, the data is sorted by expression in ascending order, which is the same as specifying ORDER BY expression ASC.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.