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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:05:39+00:00 2026-05-23T04:05:39+00:00

I have the following table id name address empid 1 AA aa 0 2

  • 0

I have the following table

id name address empid

1  AA   aa      0
2  BB   bb      0
3  CC   cc      0

I need to write a query to set empid starting from 1. How to write it please. Do i have to use a stored procedure to that or can do it with a normal query?

Thank You.

  • 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-23T04:05:40+00:00Added an answer on May 23, 2026 at 4:05 am

    Here is a way to do it that utilizes a pretty obscure assignment operator in MySQL. This solution won’t skip numbers in the case of gaps in the primary key sequence like some of the other solutions.

    set @count = 0;
    update test set empid = @count := @count+1;
    

    Here is the proof:

    mysql> create table test (
        -> id int unsigned primary key auto_increment,
        -> name varchar(32) not null,
        -> address varchar(32) not null,
        -> empid int unsigned not null default 0
        -> ) engine=innodb;
    Query OK, 0 rows affected (0.02 sec)
    
    mysql> insert into test (name, address)
        -> values ('AA', 'aa'), ('BB', 'bb'), ('CC', 'cc');
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from test;
    +----+------+---------+-------+
    | id | name | address | empid |
    +----+------+---------+-------+
    |  1 | AA   | aa      |     0 |
    |  2 | BB   | bb      |     0 |
    |  3 | CC   | cc      |     0 |
    +----+------+---------+-------+
    3 rows in set (0.00 sec)
    
    mysql> set @count=0;
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> update test set empid = @count := @count+1;
    Query OK, 3 rows affected (0.00 sec)
    Rows matched: 3  Changed: 3  Warnings: 0
    
    mysql> select * from test;
    +----+------+---------+-------+
    | id | name | address | empid |
    +----+------+---------+-------+
    |  1 | AA   | aa      |     1 |
    |  2 | BB   | bb      |     2 |
    |  3 | CC   | cc      |     3 |
    +----+------+---------+-------+
    3 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: select column_name, count(column_name) from table group by column_name having
I have a table with the following values: ID NAME ADDRESS 1 Bob Super
I have the following query in access 2003 mdb. SELECT Company.Name, Company.Address, Place.Name_of_Place, Sum(Income.Value)
Let's say I have the following table: CustomerID ParentID Name ========== ======== ==== 1
If I have the following existing table schema +-------------------+ +-------------------+ | Address | |
I have a SQL Table with the following columns: PROFILE ---------------------------------- Name | DateOfBirth
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have the following code: <table id=box-table-a class=tablesorter> <thead> <tr> <th scope=col>B-House/Dorm Name</th> <th
I have the following code showing location objects from an NSArray in a table,
I have two table and I need to copy the data across from SRCServiceUsers

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.