alternating default value for new field in mysql database - mysql

I have a table of people, around 50K+ records. I need to add a new field 'given_number' to this table that defaults to an alternating 1, 2 or 3.
e.g.
id 1 - given_number = 1
id 2 - given_number = 2
id 3 - given_number = 3
id 4 - given_number = 1
id 5 - given_number = 2
id 6 - given_number = 3
id 7 - given_number = 1
id 8 - given_number = 2
id 9 - given_number = 3
What's the best way of doing this?

Being given_number always a calculation you can make from id (id plus 1 modulo 3 plus 1), I think you don't need to actually have that field in the table, but ask for that calculation whenever you need it, and name that calculation given_number:
SELECT (id+1)%3+1 AS given_number, ... FROM ...

I will suggest you to add column first then by using sql you need to set the value for newly added field

I don't think i have a best solution but may be you can try this:
Add column with default set to 1
then update and increment all rows by 1 whose Id is divisible by 2
in same way increment all rows by whose id is divisible by 3.

Related

Updating a table to increment a value per user id

I have the following table (called node_items):
id node_id position
== ======= ========
1 1 1
2 1 1
3 2 1
4 2 1
5 2 1
6 2 1
7 3 1
8 3 1
9 3 1
10 3 1
The position field is supposed to mark an items position in the node. They are currently all set at 1. I am wanting to fix this table by setting an incremental position for each item per node_id. So, the result I am after is:
id node_id position
== ======= ========
1 1 1
2 1 2
3 2 1
4 2 2
5 2 3
6 2 4
7 2 5
8 3 1
9 3 2
10 3 3
So I somehow need to group each item by node_id and set the position to an initial value of 1 and increment it for each subsequent item with that node_id.
Note: To keep my explanation simple, the above node_ids are shown in order (1,1, 2, 2,2,2,2,3,3,3), but this isn't usually the case.
What query can I use to update the position values incrementally for each node_id?
Fixing the table once can be done with an UPDATE:
SET #n = 0, #p = 1;
UPDATE node_items
SET position = (#p := IF(node_id=#n, #p+1, 1)),
node_id = (#n := node_id)
ORDER BY node_id, id;
Making the table maintain the position values as you insert/update/delete data is harder. Basically, can't do it while allowing concurrent updates. You have to lock the table in every session that needs to do writes to the table. This makes concurrent sessions run serially.
You can read my old post about this here: Some sort of “different auto-increment indexes” per a primary key values

Resequence column numbers in MySQL

I have a table that has a column with numbers in a sequence, but for some numbers they can be in groups with several of the same number. Then when the user deletes out a group, there can be a gap in the numbers.
What I want to do is re-sequence that column so it starts at 1, then proceeds up to the last column. So for example, the table column named Item could look like this:
1
1
2
3
5
5
7
8
8
and I want it to convert to this:
1
1
2
3
4
4
5
6
6
Is there a way to do this in MySQL?
I wouldn't encourage to do this, but you can re-number your values after each update
set #cnt = 0;
update test t set t.number=#cnt:=#cnt+1;
Update: this will increment the number field by one

How to update fields of one table using other table?

How can we update table 1 such that it will replace b field value of table 1 by that of table 2 where a field value are found same?
Suppose I have two tables
table 1
fields a b c
1 5 10
1 5 8
2 5 0
1 4 11
and
table 2
fields a b
1 6
1 7
2 5
1 4
I'm going on 6th form knowledge so I'll leave the code for you to do, but here's basically how I'd do it:Select all values from table 2For each value, select the rows from table 1 with the matching 'a' valueCount number of matching valuesIf it's over one, update table 1 set 'b' as the new value where 'a' matches
Edit: Oh, just realised, the 'a' values aren't unique, unless both tables have matching ID for each row, I'm not sure you can do it.

Autoincrement in mysql without unique ids

I have a table with main-ids and user-ids. Each user-id has a set of their own unique main-ids, but multiple user-ids can have the same main-id. Is there anyway to increment a main-id for a specific user without having to do 2 queries?
If you mean like this:
User ID Main ID
1 1
1 2
1 3
2 1
2 2
2 3
Then you're going to need to make an INSERT trigger that finds the next MainID for that user and stores that.

Updating a table in a Mysql database

Let's say I have in a table named threadloc:
id thread
4 1
3 2
2 3
1 4
for a table
I want to change the table value of thread so that I can pick any thread and put in on the bottom (id 1) and push all the other threads up one.
So like I pick 2 it would be:
id thread
4 1
3 3
2 4
1 2
UPDATE threadloc SET ID = ID + 1 WHERE thread <> #currentThread AND ID < #currentID;
UPDATE threadloc SET ID = 1 WHERE thread = #currentThread
edit: now it doesn't change higher IDs