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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:10:52+00:00 2026-05-27T21:10:52+00:00

There is a table with an int field – field_1. I want to insert

  • 0

There is a table with an int field – field_1.
I want to insert a new row.
The field_1 value will be Maximum value from all the entries plus one.
I’ve tried:

INSERT INTO table (field names, `field_1`) 
VALUES (values, '(SELECT MAX(field_1) FROM table)');  

I get ‘0’ in the field_1.
I know I can do it in separate queries.
Is there a way to perform this action with one query? I mean one call from php.

I have an auto-increment field ‘id’ and I want to add ‘position’ field. I want to be able to make changes in position but the new item will always have highest position

  • 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-27T21:10:53+00:00Added an answer on May 27, 2026 at 9:10 pm

    Whatever it is that you are trying to do, it will not work, because it is not guaranteed to be atomic. So two instances of this query executing in parallel are guaranteed to mess each other up at some random point in time, resulting in skipped numbers and duplicate numbers.

    The reason why databases offer auto-increment is precisely so as to solve this problem, by guaranteeing atomicity in the generation of these incremented values.

    (Finally, ‘Auto Increment Manually’ is an oxymoron. It is either going to be ‘Auto Increment’, or it is going to be ‘Manual Increment’. Just being a smart ass here.)

    EDIT (after OP’s edit)

    One inefficient way to solve your problem would be to leave the Position field zero or NULL, and then execute UPDATE table SET Position = Id WHERE Position IS NULL. (Assuming Id is the autonumber field in your table.)

    An efficient but cumbersome way would be to leave the Position field NULL when you have not modified it, and give it a value only when you decide to modify it. Then, every time you want to read the Position field, use a CASE statement: if the Position field is NULL, then use the value of Id; otherwise, use the value of Position.

    EDIT2 (after considering OP’s explanation in the comments)

    If you only have 30 rows I do not see why you are even trying to keep the order right on the database. Just load all rows in an array, programmatically assign incrementing values to any Position fields that are found to be NULL, and when the order of the rows in your array changes, just fix the Position values and update all 30 rows in the database.

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

Sidebar

Related Questions

Let's say there's a table created as follows: create table testTable ( colA int
in an sql table there's an id, first name and last name field. i'd
I'm storing timestamp as int field. And on large table it takes too long
I have a table: id:int revision:int text:ntext In general I will need to retrieve
SQLServer int field. Value sometimes null. DataAdapter fills dataset OK and can display data
I'm working with a table and there is field in my table which stores
I need to update a field in my production db from INT to VARCHAR.
I have an int field in my table scores . It can be a
Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
Imagine I have these columns in a table: id int NOT NULL IDENTITY PRIMARY

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.