-1

i want to make query that can auto increment in queue_number not just number 1 but every time i insert values into queue_number I want increment by + 1, and services column not change but just in queue_number

right down here just my basic code can you help me with this :)

INSERT INTO mytable (services, queue_number) values ('1', '1');
5
  • 1
    Can you add AUTO_INCREMENT to the services column definition? Commented Nov 21, 2016 at 13:50
  • the value in servics just 1 but i want to increment just like serial number Commented Nov 21, 2016 at 13:54
  • in queue_number Commented Nov 21, 2016 at 13:55
  • Possible duplicate of How to generate a dynamic sequence table in MySQL? Commented Nov 21, 2016 at 14:05
  • but my queue_number coloumn doesn't have AUTO_INCREMENT feature just INT type Commented Nov 21, 2016 at 14:17

2 Answers 2

1

You need to define the field as AUTO_INCREMENT:

queue_number INT NOT NULL AUTO_INCREMENT

On the insert, you don't need to specify it, it will auto-increment by 1 each time:

INSERT INTO mytable (services) values ('1');
Sign up to request clarification or add additional context in comments.

Comments

0

MySQL has the option 'ON DUPLICATE KEY UPDATE' that can do the trick.

First of all, make sure you have an unique index or primary key on the field "services":

ALTER TABLE mytable ADD UNIQUE INDEX 'services_key' ('services');

Then, when making the insert:

INSERT INTO mytable (services) values ('1') ON DUPLICATE KEY UPDATE queue_number=queue_number+1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.