update value in comma separated and replace with value in query - mysql

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.

Related

Only update a single row in a query based on non-unique parameters?

I have a big table with duplicate keys that I am trying to connect to smaller table that has unique keys. I know for a fact there will not matches for everything. I only want a match from my smaller table to update a single row in the bigger table and then to move onto the next smaller table row for the next update. I need it like this because I am trying to create unique id's in the larger table as each row represents a real world product which has it's own heiarchy of real world objects.
So for example,
bigtable
barcodeSnippet t_stamp workId parentCase newId
aaaa time1 1 1 NULL
aaaa time1 1 1 NULL
aaaa time1 1 1 NULL
and my small table might have this
smalltable
id barcodeSnippet t_stamp workId parentCase
1 aaaa time1 1 1
2 aaaa time1 1 1
the end result I want in my bigtable is
bigtable
barcodeSnippet t_stamp workId parentCase newId
aaaa time1 1 1 1
aaaa time1 1 1 2
aaaa time1 1 1 NULL
where I only mached once per row, and was left over with a NULL since I had 3 rows in the big table and two matches in my smaller one.
My current query
UPDATE bigtable as bt
JOIN smallTable as st ON (bt.barcodeSnippet = b.barcodeSnippet AND
bt.parentCase= st.parentCase and bt.t_stamp = st.t_stamp and bt.workId =
st.workId)
SET bt.bottlesId = st.id;
does not work, and I don't see it's possible to use the LIMIT in a UPDATE for MySQL. I have seen other answers in MS SQL where you can use TOP 1, perhaps where newId IS NULL, but again I am using MySQL here.
I am thinking I might need to use a Stored Procedure/Cursor approach but even with that it seems like I will run into the issue of having to run an update statement and then I am back at square 1.
Any ideas? Using MySQL 5.6.
EDIT: Think I have a decent solution. I just updated with my query so I do have duplicates. However, now I added a row number column. I plan to join the table on itself and update it if the row number is < the row number, therefore I keep the top ID and can turn the others to null, which is suitable.
Something like this
UPDATE bigtable tb
JOIN bigtable tb2 ON tb.newId = tb2.newId
SET tb.newId = NULL
WHERE tb.rowNumber < tb2.rowNumber;
You use the auto_incremented id column with the superglobal $_GET:
1.php
<?php $var = $row["id"]; ?>
Something
Then in your 2.php
<?php $id = $_GET["id"];
$sql = "SELECT * FROM table WHERE id='$id';";
Of course you need to use prepared statements

MySQL updating duplicate IDs based on match and no match criteria all in one table

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;

Query which Find string and increment the count

I have table like that,
id name count
1 rrr 2
2 www 3
3 qqq 4
4 aaa 5
5 gyhhh 4
6 dfgdfg 5
I want to write the query which find the name in table and if it find then increment the count in count column for that name. The count maintain the no of time name used by the user.If user used the name , then I am check the name in db , if it found then I want to update row with increment in count.
A simple update query required:
If you want to increase count only if the input parameter exactly matches the name then use this:
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` = ?
And if you want to increase count if the input parameter is a substring of name then you can use LIKE
UPDATE your_table
SET `count` = `count` + 1
WHERE `name` LIKE CONCAT('%',?,'%')
Note: Replace the question mark (?) by your input parameter.
Try this:
select id,name, id + 1 from
(Select id,name from table_name where name in('sa','da','ba','ca')) as a;
hope it helps..

mysql update between row and shift current to right

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

how to update a specific field(column) of a row by that row's autoincrement value with some prefix in mysql?

i have below table
it_id item_id item_name
1 ite_1 shirt
2 ite_10 pant
3 ite_11 socks
4 ite_12 shoes
5 ite_13 belt
now i need to change item_id with with ite_(value of it_id of that row)
means if it_id=x then item_id should be ite_x and so on...
what will b sql query for that??
UPDATE ###
if i want to change it_id with a prefix means
new it_id=ite_x where x is it's(it_id's) previous value(1,2,3,4,5 etc....) and it_id is not an auto increment field
update table_name SET item_id=CONCAT('ite_', id)
SORRY your column id it_id, so it should be (Not Sure Though)
update table_name SET item_id=CONCAT('ite_', it_id)
I think you can do it with follwing steps
1] Change datatype of it_id to VARCHAR
2] your query is
update table_name SET it_id=CONCAT('ite_', it_id)