2023-02-21

MySQL- INDEX(): How to Create a Functional Key Part Using Last nth Characters?

How would I write the INDEX() statement to use the last Nth characters of a functional keypart? I'm brand new to SQL/MySQL, and believe that's the proper verbiage of my question. explanation of what I'm looking for is below.

The MySQL 8.0 Ref Manual explains how to use the first nth characters, showing that the secondary index using col2's first 10 characters, via example:

CREATE TABLE t1 (
  col1 VARCHAR(40),
  col2 VARCHAR(30),
  INDEX (col1, col2(10))
);

However, I would like to know how one could form this using the ending characters? Perhaps something like:

...
 INDEX ((RIGHT (col2,3)));
);

However, I think that says to index over a column called 'xyz' instead of "put an index on each column value using the last 3 of 30 potential characters"? That's what I'm really trying to figure out.

For some context, it'd be helpful to index something with smooshed/mixed data and am playing around as to how such a thing could be accomplished. Example of the kind of data I'm talking about, below, is a simplified, adjusted version of exported data from an inventory/billing manager that hails from the 90's that I had to endure some years back...:

Col1 Col2
GP6500012_SALES_FY2023_SBucks_503_Thurs R-DK_Sumat__SKU-503-20230174
GP6500012_SALES_FY2023_SBucks_607_Mon R-MD_Columb__SKU-607-2023035
GP6500012_SALES_FY2023_SBucks_627_Mon-pm R-BLD_House__SKU-503-20230024
GP6500012_SALES_FY2023_SBucks_929_Wed R-FR_Ethp__SKU-929-20230324

Undoubtedly, better options exist that bypass this question altogether- and I'll presumably learn those techniques with time in my data analytics coursework. For now, I'm just curious if it's possible to somehow index the rows by suffix instead of prefix, and what a code example would look like to accomplish that. TIA.



No comments:

Post a Comment