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

The Archive Base Latest Questions

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

I have a table ( foo ) where I already have a PK on

  • 0

I have a table (foo) where I already have a PK on id:

id      name      rank
-----------------------
 1      AAAA        2
 2      BBBB        1

I want to insert a new row where I know the values of column id and name and want rank to take a value greater than any other value in the same column in preceding rows (similar to what auto_increment does for us).

i.e. if I were to add a row with value = CCCC, the rank column should have a value 3. I need to do this in a compound statement if possible. I tried the following which does not work.

insert into foo (`name`, `rank`)
values ('CCCC', (select max(`rank`) from `foo`))

Which gives me the following error:

You can't specify target table 'foo' for update in FROM clause

Note: I would ideally like to have the rank column as an auto_increment field, but apparently that’s not allowed either, since I already have a PK.

PS: I need to be able to execute this statement from PHP without using stored procedures.

  • 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-27T13:52:32+00:00Added an answer on May 27, 2026 at 1:52 pm

    Try this first, it being derived instantly from your post:

    INSERT INTO foo (`name`, `rank`)
    SELECT 'CCCC', (MAX(`rank`) + 1) AS rank
    FROM `foo`
    

    Then using PDO, I think this’ll work:

    ...
    $sql = "INSERT INTO foo (`name`, `rank`) SELECT ?, (MAX(`rank`) + 1) AS rank FROM `foo`"
    $name = "CCCC";
    $st = $pd->prepare($sql);
    $st->bindValue(1, $name);
    
    try {
        $retval = $st->execute();
    } catch (PDOException $pdoex) {
    ...
    

    Not sure if I got in syntactically correct but that should be about the gist of it … I think

    Err.. lemme know if the SQL works, at least 😀

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

Sidebar

Related Questions

I have a (pre-existing) table witha column 'foo'. I want the model to have
I have a list of values, and want to know which ones don't already
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT
Let's say I have SELECT name FROM table which gives me something like foo
Let's say I have a table Foo . This table has the columns ID
I have my own type: CREATE TYPE MyType AS TABLE ( foo INT )
I have a table A with 2 columns Foo and Bar. I have a
In my Ruby on Rails app, I have a User table and a Foo
Suppose I have a database table with two fields, foo and bar. Neither of
Let's say I have a mysql table, called foo with a foreign key option_id

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.