0

In my example I have this table :

| name | number   |
-------------------
| abc  |          |
| bca  |          |
| sad  |          |
| tyu  |          |
| hjh  |          |
| lpk  |          |
| ass  |          |
| drc  |          |
| dfg  |          |

then i get some variable filled with number like :

$order = 3, then i want to make query to update the table above to look like this :

| name |  number  |
--------------------
| abc  |     1    |
| bca  |     2    |
| sad  |     3    |
| tyu  |     1    |
| hjh  |     2    |
| lpk  |     3    |
| ass  |     1    |
| drc  |     2    |
| dfg  |     3    |

How do I do that in mysql query??

Thanks in advance guys

2
  • Ok and what you tried so far? Commented Nov 1, 2017 at 8:33
  • Use modulus logic Commented Nov 1, 2017 at 8:39

1 Answer 1

2
SET @order=3;
UPDATE Table1 SET number2=MOD(number-1,@order)+1;
Sign up to request clarification or add additional context in comments.

2 Comments

...which begs the question; why store this at all?
works like a charm, but what if the number filed is not relevant now. so all i need is 1 number field

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.