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

  • Home
  • SEARCH
  • 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 9139693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:25:15+00:00 2026-06-17T09:25:15+00:00

I have this table built like this, which assigns a unique articleId starting with

  • 0

I have this table built like this, which assigns a unique articleId starting with zero for each user.

CREATE TABLE `articles` (
  `artcId` INT(10) NOT NULL AUTO_INCREMENT,
  `artcUserId` INT(10) NOT NULL DEFAULT '0',
  `artcStackId` INT(10) NOT NULL DEFAULT '0',
  `artcTitle` VARCHAR(200) NULL DEFAULT NULL,
  PRIMARY KEY (`artcUserId`, `artcId`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM;

There also is a part which assigns a running serial number (artcStackId) to each article. You’ll see in the code.

Thing is, is it somehow possible to assign a artcStackId based on the country of the user? That country code will come from php.

Eg: If it’s US then start from 10001+1, if its UK start from 20001+1, if its CA then start from 30001+1 and on & on.

Will it be possible to do this?

My current SQL query goes as follows:

insert into articles (artcUserId, artcStackId, artcCountry, artcTitle)
select 4,IFNULL((MAX(artcStackId)+1) ,0),'US','Hello World'
FROM articles;

But the way I want it it’ll have to go like (this is just an example sql):

insert into articles (artcUserId, artcStackId, artcCountry, artcTitle)
select 4, IF artcCountry = 'US' then (selct MAX(artcStackId)+1
                                      where artcCountry = 'US'),
       'US','Hello World'
FROM articles;

I hope you get the idea.

enter image description here
enter image description here

  • 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-17T09:25:16+00:00Added an answer on June 17, 2026 at 9:25 am

    I would suggest you to use a TRIGGER:

    CREATE TRIGGER ins_articles
    BEFORE INSERT ON articles
    FOR EACH ROW
      SET new.artcStackId=coalesce(
        (select max(artcStackId)
        from articles
        where artcCountry = new.artcCountry)+1,1)
    ;
    

    or something like this:

    CREATE TRIGGER ins_articles
    BEFORE INSERT ON articles
    FOR EACH ROW
      SET new.artcStackId=coalesce(
        (select max(artcStackId)
        from articles
        where artcCountry = new.artcCountry)+1,
    
        case when new.artcCountry = 'US' then 10001
             when new.artcCountry = 'UK' then 20001
             when new.artcCountry = 'CA' then 30001
        end)
    ;
    

    see it working here. Then you can insert without specifying the column artcStackId, since it will automatically be calculated.

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

Sidebar

Related Questions

I have built a table from which I allow the user to add and
I have an HTML table containing a few rows (this is built dynamically). All
I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have this table, which can be seen as a basic custom gantt chart:
I have this table And my goals is to have a table looking like
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have an in-memory table that might looks something like this: Favorite# Name Profession
I have a table named Sample which looks like. I want the output as
I have a data table, and this corresponding pivot table: For each row in
Lets say I have a view file that is built like this: <html> ...

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.