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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:23:37+00:00 2026-05-29T23:23:37+00:00

I’m adding a field to my table called room_num. Basically upon insert I’d like

  • 0

I’m adding a field to my table called “room_num”. Basically upon insert I’d like to set the default value of the room_num to be the max value of the last room_num + 1. Obviously I’d use auto-increment if this table applied to only one user but, it will apply to many so I have a running unique index as well as a unique field user_index.

I could run a query based on the user_index and return the max for “room_num” but, since I’m already here, is there a way to make the default value some sort of function?

So, for example my table looks like this:

+-------+-------------+---------+-------------+
| index |  room_num   |  title  |  user_index |
+-------+-------------+---------+-------------+
| 0     | 0           | Patio   | 10          |
+-------+-------------+---------+-------------+
| 1     + 1           | Dining  | 10          |
+-------+-------------+---------+-------------+
| 2     + 0           | Bar     | 15          |
+-------+-------------+---------+-------------+
| 3     + 2           | Dining  | 10          |
+-------+-------------+---------+-------------+

So, now I want the default value for room_num of my next insert for user_index 15 to default to max room_num (0) + 1 (which in this case would be 1). But, I’d like to take care of this here:

enter image description here

Rather than querying for it in my application first and THEN inserting the record and in essence making two database connections.

Is this possible?

  • 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-05-29T23:23:39+00:00Added an answer on May 29, 2026 at 11:23 pm

    Well, you could do something like this:

    INSERT INTO myTable (title, user_index, room_num)
      SELECT 'MyTitle', 10, COALESCE((MAX(room_num) + 1), 1)
      FROM   myTable
      WHERE  user_index = 10
    

    Obviously, from your application you would just replace 'MyTitle' and the instances of 10 with your desired title and proper user_index.

    Note: Based on my five minutes with Google, I don’t think INSERT...SELECT is an atomic operation. So, you’ll probably want to wrap this in a transaction or table lock/unlock to prevent race conditions.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.