I have a table having column name ID(primary key).
in this I have values from 1 to 152.
now I want to update the table so that the id ranging from 109 to 152 incremented by 1.
when I run this sql
update category set id=(id+1) where id<152 and id>108
it give error because when it update ID=109 to 110 then it found duplicate of 110 in next row.
so how can I do that.
can I start updating from last row that is 152 and stop it on 108.
I am guessing that you are using MySQL. If so, you can do this by using order by.
update category
set id = id + 1
where id > 108 and id < 152
order by id desc;
Assuming the ids are positive, the following should work in any database (assuming the column is not declared "unsigned"):
update category
set id = -(id + 1)
where id > 108 and id < 152;
update category
set id = -id
where id < 0;
Do it in two steps. First add 10,000 (or any other unused range) and then subtract 9999. Do not forget to adjust the criteria in the second step.
Related
I had two tables Table 1 & Table 2 AS shown here
Table:1
ID
IMG_PATH
CAT_ID
166
hfhbf
1
164
jgj
2
162
ggd
1
160
mfnf
1
158
dbd
2
Table:2
ID
IMG_PARENT_ID
Here I want to print table 1's ID column data Example:166
Here (ID-1) Example:165
Here I want to print table 1's ID column data Example:164
Here (ID-1) Example:163
Here I want to print table 1's ID column data Example:162
Here (ID-1) Example:161
Here I want to print table 1's ID column data Example:160
Here (ID-1) Example:159
Here I want to print table 1's ID column data Example:158
Here (ID-1) Example:157
AS SHOWN IN TABLE 2 I NEED FOLLOWING VALUE...
and dont try this manually method:
INSERT INTO tabla2
SELECT * FROM tabla1
WHERE id = 1 //Here we write the condition
I want to fetch data because arround 10,000's row are inserted in this table
Lots of tries but didnt get it
based on what you provided info about your question, this is what I understand about this.
Assuming that table 1 is auto_increment with ID of 1-10,000.
Then you can use this to select the even IDs in table 1 and insert it to table 2
insert into table2 (ID) select ID from table1 group by ID having mod(ID, 2) = 0;
To select odd IDs from table 1 and insert it to table 2 you can use this
insert into table2 (IMG_PARENT_ID) select ID from table1 group by ID having mod(ID, 2) = 1;
Hopefully I can explain this clearly. I have a table that has what need to be unique IDs for people within a group. The IDs are generated using first 3 letters of the first name and date of birth. Normally, with smaller groups (less than 500) this works fine. However in large groups we do hit upon some duplicates. We'd then just append a -1, -2, -3 etc. to any duplicate IDs. For example:
ID GROUP UID FIRST_NAME
1 123456 ALE19900123 ALEXIS
2 123456 ALE19900123 ALEXANDER
3 123456 ALE19900123 ALEJANDRO
4 789789 ALE19900123 ALEX
What I'd like to do is for ID 2 and 3 append a -1 and -2 respectively to their UID field so that 1,2 and 3 are now unique (GROUP + UID). ID 4 would be ignored because the GROUP is different
I've started with something like this:
UPDATE table A
JOIN table B
ON B.GROUP = A.GROUP
AND B.UID = A.UID
AND B.FIRST_NAME <> A.FIRST_NAME
AND B.ID < A.ID
SET A.duplicate_record = 1;
That should set the duplicate_record field = 1 for IDs 2 and 3. But then I still need to append a -1, -2, -3 etc. to those UIDs and I'm not sure how to do that. Maybe instead of just setting a flag = 1 for duplicate I should set the count of records that are duplicates?
If group, UID tuple is unique (and it should be), why not insert ignore the first one (without any value appended), check for how many rows were affected by SELECT ROW_COUNT();, and if that is zero, append -1? If you put it in a for cycle (pseudocode):
while i < 1000 do
insert ignore into people (group, uid, first_name) values (123456, concat(their_uid, "-", i), first name);
if ((select row_count();) == 1):
break;
i=i+1;
end while;
iam having table like this
id name sol_id
1 abc 2,5,8
2 dt 5,9,10
here i want to add some value(10) to id=1 of sol_id,so value 10 will be added with id=1 and at the same time value 10 of id=2 replace with some empty value i want output like this
id name sol_id
1 abc 2,5,8,10(here updating)
2 dt 5,9 (10 removing)
i wrote query like this but its performs one operation not both
UPDATE my_table SET sol_id=REPLACE(sol_id,',10,',',')
and sol_id = Concat(sol_id, ',', 10) where id = 1
is it possible? Thanks in advance
It is possible, but clunky.
Basically you would do
UPDATE table SET SET field = CASE id
WHEN 1 THEN <formula for the case of id=1>
WHEN 2 THEN <formula for the case of id=2>
END
WHERE ID IN (1, 2);
There's no great advantage over running several queries inside a TRANSACTION and, if necessary, a suitable lock on the table.
I have this table structure:
Table1: id, group_id, label, sort_order
Keys: PK: id, UNIQUE: group_id+sort_order, INDEX: group_id
Sample data:
id group_id label sort_order
50 1 Field 1 1
51 1 Field 2 2
52 1 Field 3 3
Where (group_id, sort_order) is a unique key.
To update the arrangement of records on front-end, the sort_order field is used in order by clause. User can update the order by dragging labels around in front-end, which passes the ids, ex: 52, 51,50 which is then treated as the new order/sorting.
Expected result:
id group_id label sort_order
50 1 Field 1 3
51 1 Field 2 2
52 1 Field 3 1
The process should update all 3 records with the new sort_order values. Sample update query:
UPDATE Table1 SET sort_order = 1 WHERE id = 52 AND group_id = 1
However, since sort_order is part of a unique key, it throws error since group_id,sort_order key already exists.
I did a hackish workaround where I update the sort order twice, ex: by updating it into a higher value, ex: sort_order = actual_order + 1000 then update again using the actual values, ex: sort_order = actual_order.
What would be the better approach? Should I just remove the unique key completely instead?
--
EDIT
--
To give a better view of the problem, here is my PHP code:
Sample fieldIds: 52,51,50 or 50,51,52 or 51,52,50. The sequence determines the new sorting.
public function updateSort($projectId, $groupId, $subgroupId, array $fieldIds)
{
// Other stuff
// ...
$statement = create_a_prepared_statement(); // ...
// Updating sort_order directly would result to unique key violation
// so change the sort_order into some negative numbers and re-sort with
// the actual orders
foreach ($fieldIds as $sortIndex => $fieldId) {
$sort = $sortIndex - 1000;
$statement->execute(array(
'id' => $fieldId,
'project_id' => $projectId,
'group_id' => $groupId,
'subgroup_id' => $subgroupId,
':0' => $sort,
));
}
// Now sort it correctly
foreach ($fieldIds as $sortIndex => $fieldId) {
$sort = $sortIndex + 1;
$statement->execute(array(
'id' => $fieldId,
'project_id' => $projectId,
'group_id' => $groupId,
'subgroup_id' => $subgroupId,
':0' => $sort,
));
}
return true;
}
Considering what you wrote in this statement:
I did a hackish workaround where ...
I would perform the first half of that phrase,
sort_order = actual_order + 1000
and then stop (meaning you are done).
You will never in 100 years experience an overflow on the max value. If that is of concern, have your annual event (via Create Event) remind you how far from concern it is, and reign that group back to 1+, when it is.
But you would need to update any given group 2M times for an overflow.
When you have such a strict constraint, you will have to use an intermediate value:
UPDATE Table1 SET sort_order = -1 WHERE id = 52 AND group_id = 1;
UPDATE Table1 SET sort_order = 3 WHERE id = 50 AND group_id = 1;
UPDATE Table1 SET sort_order = 1 WHERE id = 52 AND group_id = 1;
-1 is the intermediate value.
The whole process:
Place sort_order for given group for record 1 at intermediate value;
Place sort_order of record 2 to desired value;
Place sort_order of record 1 to desired value;
So to keep the constraint you will always have to update 2 records, and 1 record even 2 times.
From the looks of it, your front end is generating update statements that update one row at a time. If at any time an update statement causes 2 rows to have the same values a unique key violation will result. I think you should look into finding a way to update all rows with sort values in one statement. I'm not familiar with mysql but I am familiar with sql server. In sql server I would copy the table to a temp table and figure out the new sorting there. Then perform a update statement to copy the new sort values back to the original table in one statement.
You can use a case statement to update all at once
UPDATE Table1 set sort_order =
CASE
WHEN id = 50 THEN 3
WHEN id = 51 THEN 1
WHEN id = 52 THEN 2
END
how to update column value of specific id and shift after to right.
id track
1 3
2 5
3 8
4 9
want to update id 3 track column value to 10, result like this
id track
1 3
2 5
3 10
4 8
5 9
id column is auto_increment
or any suggestion it's my pleasure.
thank you.
You should avoid tweaking auto_increments. Auto increment keys are usually supposed to be used internally (e.g. for linking purposes). If you want to order tracks, i suggest you add a seperate numeric field "ordernro" to the table and update that
To add a column order nro to a table named album, do like this:
alter table album add ordernro int(2) after id;
Then copy the current value for id into this new column:
update album set ordernro=id;
(do this only once after adding the column)
To insert track 10 at position 3 first shift the rows:
update album set ordernro = ordernro + 1 where ordernro >= 3;
And then insert track 10:
insert into album (ordernro, track) values (3, 10);
Remember to update your existing insert/update/select statements accordingly.
The result can be checked by:
select * from album order by ordernro;
(The id will now be "mixed up", but that doesn't matter)
UPDATE table SET id = id + 1 WHERE id >= x;
x being the id where you place your current track.
The problem with JK 's answer is that MySQL returns error saying that is can't UPDATE because the index at x+1 would be duplicate.
What I did is
UPDATE table SET id = id + 100 WHERE id >= x;
UPDATE table SET id = id - 99 WHERE id >= x;
And then INSERT my row at index x