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 8029075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:19:48+00:00 2026-06-05T00:19:48+00:00

I have a simple table in mysql that has different types of records, differentiated

  • 0

I have a simple table in mysql that has different types of records, differentiated by a values in the column ptype

my table looks like this

id1…ptype..usr…item
1…..43…….2……7001
2…..44…….2……8001
3…..43…….2……7002
4…..43…….2……7003
5…..43…….3……7001

When I add a new record, I need my query to insert an auto incremented value in the item column, based upon ptype and specific to usr. i.e. if i insert a new record
id1…ptype..usr…item
6…..43…….3……?

it would add 1 to the highest number existing for ptype=43 and usr=3
id1…ptype..usr…item
6…..43…….3……7002

if we added another record for ptype=44 and usr=2
id1…ptype..usr…item
7…..44…….2……8002

i think i should do this by initially inserting the new record with item blank and then updating that record with information derived from the new record(i.e. @lastid) using the CASE WHEN THEN method, but it’s not working.

SET @lastid := LAST_INSERT_ID();

SET @ptype =  (SELECT `ptype` FROM a1 WHERE `id1` = @lastid);

SET @item =  (SELECT (
CASE
when @ptype = 41 then (SELECT 1 + coalesce((SELECT max(`item`) FROM `a1` WHERE `ptype` = 41 AND `plate`=7 AND `userid` = @userid), 5000))
when @ptype = 42 then (SELECT 1 + coalesce((SELECT max(`item`) FROM `a1` WHERE `ptype` = 42 AND `plate`=7 AND `userid` = @userid), 6000))
when @ptype = 43 then (SELECT 1 + coalesce((SELECT max(`item`) FROM `a1` WHERE `ptype` = 43 AND `plate`=7 AND `userid` = @userid), 7000))
when @ptype = 44 then (SELECT 1 + coalesce((SELECT max(`item`) FROM `a1` WHERE `ptype` = 44 AND `plate`=7 AND `userid` = @userid), 8000)) 
when @ptype = 45 then (SELECT 1 + coalesce((SELECT max(`item`) FROM `a1` WHERE `ptype` = 45 AND `plate`=7 AND `userid` = @userid), 9000)) 
when @ptype = 46 then (SELECT 1 + coalesce((SELECT max(`item`) FROM `a1` WHERE `ptype` = 46 AND `plate`=7 AND `userid` = @userid), 10000)) 
ELSE 0
end) as item
from
a1 WHERE `id1` = @lastid);

UPDATE a1 SET item = @item WHERE id1 = @lastid

as is, @item is returning values of 0 initially, no matter what ‘ptype’ the new record has, and is incrementing by 1 for subsequent entries…. i need the first record added in each ptype to be 5001 6001, 7001, etc.

  • 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-05T00:19:51+00:00Added an answer on June 5, 2026 at 12:19 am

    First, the answer for which you didn’t ask: reverse your idea by creating the rows in their own tables (with an AUTO_INCREMENT as eggyal suggested) and then move the data to this table.

    And now the answer:

    Your information is a bit mis-matched, which might explain the problem or just be a red herring. For example, you don’t describe what ‘plate’ is, but you use it in your query. You also use @userid, which is not set in your examples.

    I created a table that seemed to match your data at the top:

    create table a1 (
      id1 int primary key auto_increment,
      ptype int,
      usr int,
      item int
    );
    

    Then set the variable that you seemed to want:

    set @userid = 2;
    set @ptype = 43;
    

    and inserted a row:

    insert into a1 (ptype, usr) values (@ptype, @userid);
    

    pulled the id back out as you did:

    SET @lastid := LAST_INSERT_ID();
    

    Then you can get the max ‘item’:

    select max(item) from a1  WHERE `ptype` = @ptype AND `usr` = @userid;
    

    To handle the initial case, you wanted a default. Since you’re separating the ptypes by 1000, you can use that:

    SELECT ifnull(max(`item`),(@ptype % 40 + 2)*1000)+1 as next
    FROM `a1`
    WHERE `ptype` = @ptype
    AND `usr` = @userid;
    
    +------+
    | next |
    +------+
    | 5001 |
    +------+
    

    Note that this isn’t thread safe, so wrap it all in a transaction/trigger.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table that looks like this: Table: Designer id: integer name:
I have an incredibly simple query (table type InnoDb) and EXPLAIN says that MySQL
Using MySQL, I have a simple table that logs the IP Address that users
I have a simple MySQL table thats contains a list of categories, level is
I have a simple MySQL table for storing notification in a web with social
I have got a simple MySQL table and primary index (id) is not numbered
I have a simple table on my local server. mysql> desc table ; +-------+---------+------+-----+---------+-------+
I have a simple Message table, with 2 indexes: mysql> show keys from Message;
My MySQL table is simple. I have 3 fields: an index a url a
I have a simple table that is used to record events against specific visits:

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.