Sorting Using Substrings of Column Values
Problem
You want to sort a set of values using one or more substrings of each value.
Solution
Extract the hunks you want and sort them separately.
Discussion
This is a specific application of sorting by expression value
(see Using Expressions for Sorting). If you want to sort rows
using just a particular portion of a column’s values, extract the
substring you need and use it in the ORDER
BY
clause. This is easiest if the substrings are at a fixed position and
length within the column. For substrings of variable position or
length, you may still be able to use them for sorting if there is some
reliable way to identify them. The next several recipes show how to
use substring extraction to produce specialized sort orders.
Get MySQL Cookbook, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.