SIGN UP MEMBER LOGIN:    
ARTICLE

Working With a Sub String Index in MySQL

Posted by Arjun Panwar Articles | Databases & DBA February 01, 2012
In this article we have to explain the substring index and how to return the substring from a string.
Reader Level:

Introduction

The substring
returns the substring as specified and an index is a data structure. So, the substring index returns a substring from a string before the specified number of occurrences of the delimiter. Here we have to explain the using; there are many examples. Indexes retrieve rows from other tables when joins are performed and it shows how to use a substring to return a string and define it's functionality. In MySQL, an index is a data structure in my databse. By using the substring index to sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable key.

Prep-end a sufficient number of periods to the table name values

We have selected a column using a substring index and concatenated the name as the leftmost and as the middle from the table.

Command 

mysql-> SELECT name,
->  SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT('..',name),'.',-3),'.',1)
->  AS leftmost,
->  SUBSTRING_INDEX(SUBSTRING_INDEX(CONCAT('.',name),'.',-2),'.',1)
->  AS middle,
->  SUBSTRING_INDEX(name,'.',-1) AS rightmost
->  FROM myhostname;

sufficient number 1.gif

Get to the First and Third segments or second and fourth segments from the id values

Here the substring index segment the column values according to the given command. We we have selected a first and third segment.

Command 

mysql-> SELECT
-> id,
-> SUBSTRING_INDEX(SUBSTRING_INDEX(id,'-',1),'-',-1) AS segment1,
-> SUBSTRING_INDEX(SUBSTRING_INDEX(id,'-',3),'-',-1) AS segment3
-> FROM house1;

1 and 3 segments.gif

Command

mysql-> SELECT
  ->  id,
  ->  SUBSTRING_INDEX(SUBSTRING_INDEX(id,'-',2),'-',-1) AS segment2,
  ->  SUBSTRING_INDEX(SUBSTRING_INDEX(id,'-',4),'-',-1) AS segment4
  ->  FROM house1;


2 and 4 segment.gif

To return all field values  the right or left of a given character

Here first of all select the table "mytable9" and it contains a column with eight rows.

select mytable 9.gif

Command

We have selected a substring index with two things from a table. It  returns from the table for each fields to the right or left of a given character, use SUBSTRING_INDEX(str,c,n).

mysql -> select SUBSTRING_INDEX(name,'r',2), SUBSTRING_INDEX(name,'i',-1) FROM mytable9;

substring 2.gif

Using procedure analyses to get information

By using the procedure analyses command we get the some useful information about the data.

Command

mysql> select * from player procedure analyse(0,0)\G

procedure.gif

Index

It shows the simple index of a table. Looks like as:


index.gif

Resources

Here are some useful resources

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Become a Sponsor