Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8211965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:36:21+00:00 2026-06-07T10:36:21+00:00

I have a mysql table in which there is a column e.g. called name.

  • 0

I have a mysql table in which there is a column e.g. called name. The column data has a specific pattern nameBase+number. E.g.

name
----------
test0
test1
test2
stack0
stack1
stack2

Each time I want to add data to the column, I have to find the last number for specific nambeBase and add the new entry +number+1.

For example, if now test came, I have to add test3 to db.

My question: What is the best way to 1. check if the nameBase already exists in db(sth like contains) and 2.find the last nameBase number. E.g. here for test is 3.

Update : Everyone, one update. I finally used java Pattern class. So cool and easy. It made everything so simple. I just could add the /d to pattern and then I could check if that matches the name and could use the pattern group to easily access the second part.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:36:23+00:00Added an answer on June 7, 2026 at 10:36 am

    The real solution here is to change the database schema to split this into two columns, the name and its number. It becomes trivial then to get the aggregate MAX() via

    SELECT name, MAX(num) AS num FROM tbl GROUP BY name
    

    However,if changing it is not an option, I would recommend using REPLACE() to remove the name portion from the column value leaving only the number portion when querying, and get the aggregate MAX() of that to find the highest existing number for it:

    SELECT
      MAX(REPLACE(name, <the name to search>, '')) AS maxnum
    FROM tbl
    WHERE 
      name LIKE '<the name to search>%'
    

    Or instead of LIKE, using a regular expression, which is more accurate than LIKE (in case a name contains another name, the LIKE might match) but more expensive:

    SELECT
      MAX(REPLACE(name, <the name to search>, '')) AS maxnum
    FROM tbl
    WHERE 
      name REGEXP '^<the name to search>[0-9]+$'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have MySQL table called Files which has file_id column and file_type column (and
I have a users table in which there's a column called 'nickname', utf-8 encoded,
I have a huge MySQL table which has its rows encoded in UTF-8 twice.
I have a MySql table called reviews which stores a lot of product reviews,
I have this mysql table called comments which looks like this: commentID parentID type
i have a large mysql database table in which one column contains values ranging
I have a mysql table field set as time type which stores data in
Hi I have a field in mySql table called jobnumber which auto increments with
I have a MySQL table in which a column contains string prefixes. For instance
I have 1 table in my mysql database called clubusers which lists users emails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.