MySQL replace function not specific enough - mysql

UPDATE `link_tag`
SET `TagID` = replace(TagID, 2, 13)
I'm specifically only trying to replace 2 with 13. When I use the above for example 202 becomes 13013 because it is replacing both instances of 2 within the number 202. I only want to replace just 2 with 13, not 2 within other larger numbers.

If you specify a where clause, you'll only update the results that match the exact clause so you could try something like this:
UPDATE `link_tag`
SET `TagID` = replace(`TagID`, 2, 13)
WHERE `TagID` = 2
Now it'll only change the 2 where the TagID is actually equal to 2
A better way would be doing this:
UPDATE `link_tag` SET `TagID` = 13 WHERE `TagID` = 2

Related

mysql RC multi-thread first search and second delete, but some search only return 1 row

I use mysql to deal some data use multi-thread, first I search the data use id range, like:
select id
from xxx
where id between 1 and 1000
and accountant_time = '2021-05-31 00:00:00'
and enter_accounts_state = 1
and enter_ce_state = 2
and ebs_summary_state = 2
and is_del = 0
result id like '1, 2, 3, 4, 5, ... 1001'.
and second I will delete these match data with addtional condition confirm_state from table like sql below:
DELETE
FROM xxx
WHERE confirm_state = 3
AND id IN ( 1, 2, 3, 4, ..., 1001);
All of the id range no intersection。
I found that some thread need return 1001 rows, but only returned the first row,
I tried several times use same code and same data, but the left data also not same, the common feature is only return first row of that batch count which need return all。
When I add for update for the select sql, it works normal,
How can I understand what happens?

update value in comma separated and replace with value in query

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.

Update multiple rows with multiple 'where' clauses for each individual row

I am trying to update my table like this:
Update MyTable
SET value = 1
WHERE game_id = 1,
x =-4,
y = 8
SET value = 2
WHERE game_id = 1,
x =-3,
y = 7
SET value = 3
WHERE game_id = 2,
x = 5,
y = 2
I can do a foreach() but that will send over 50 separate Queries which is very slow.
That's why I want it to be combined into 1 big Query.
( I do use an id for each row but the combination of game_id, x and y is what I use to Identify the row I need. )
The update_batch() function from codeIgniter described here:
Update batch with CodeIgniter
was helpful and almost perfect but it only allows for 1 single where clause, you cannot (as far as I understood and tried) enter an array with multiple where clauses.
I've also checked out this question:
MYSQL UPDATE SET on the Same Column but with multiple WHERE Clauses
But it only allows for multiple row updates containing only a single different WHERE clause and I need multiple WHERE clauses! :)
Anwsers can be in simple SQL or with the use of php (and CodeIgniter) or in a different way. I'd this problem to be solved in any possible way ;)
I can really use the advice/help! =D
give this a try by using CASE
Update MyTable
SET value = CASE
WHEN game_id = 1 AND x = -4 AND y = 8 THEN 1
WHEN game_id = 1 AND x = -3 AND y = 7 THEN 2
WHEN game_id = 2 AND x = 5 AND y = 2 THEN 3
ELSE value
END
WHERE game_ID IN (1,2,3) AND -- the purpose of this WHERE clause
x IN (-4, -3, 5) AND -- is to optimize the query by preventing from
y IN (8,7,2) -- performing full table scan.

Update multiple rows with multiple where clauses for each row. (update_batch can only handle 1 where clause) [duplicate]

This question already has an answer here:
Update multiple rows with multiple 'where' clauses for each individual row
(1 answer)
Closed 9 years ago.
I am trying to update my table like this:
Update MyTable
SET value = 1
WHERE game_id = 1,
x =-4,
y = 8
SET value = 2
WHERE game_id = 1,
x =-3,
y = 7
SET value = 3
WHERE game_id = 2,
x = 5,
y = 2
I can do a foreach() but that will send over 50 separate Queries which is very slow.
That's why I want it to be combined into 1 big Query.
( I do use an id for each row but the combination of game_id, x and y is what I use to Identify the row I need. )
The update_batch() function from codeIgniter described here:
Update batch with CodeIgniter
was helpful and almost perfect but it only allows for 1 single where clause, you cannot (as far as I understood and tried) enter an array with multiple where clauses.
I've also checked out this question:
MYSQL UPDATE SET on the Same Column but with multiple WHERE Clauses
But it only allows for multiple row updates containing only a single different WHERE clause and I need multiple WHERE clauses! :)
Anwsers can be in simple SQL or with the use of php (and CodeIgniter) or in a different way. I'd this problem to be solved in any possible way ;)
I can really use the advice/help! =D
EDIT:
Sorry the question wasn't clear enough, I just changed it!
It seems me you need the logical operator AND:
UPDATE MyTable
SET value = 1
WHERE game_id = 1
AND x = -4
AND y = 8
I'm not familiar with the multiple WHERE clauses how you have it laid out, why not just do
UPDATE MyTable
SET value = 1
WHERE game_id = 1
AND x = -4
AND y = 8

MySQL add data to an existing field

I have a field enq_id - it currently contains numbers such as:
80081
414365
567
Now, I need to update the field in two ways, firstly I need to find out if any of the numbers are more than 6 characters long (there shouldn't be but I need to check). I then need to set a character limit of 6 characters on that field and then, finally, add 0's to the beginning of any enq_id that isn't 6 characters long to make it 6 characters.
Any ideas?
If the following returns the result you're trying to achieve:
SELECT
`enq_id`,
IF(CHAR_LENGTH(`enq_id`) < 6,
LPAD(`enq_id`,6,'0'),
SUBSTRING(`enq_id`,1,6)
) AS 'result'
FROM `some_table`
WHERE CHAR_LENGTH(`enq_id`) != 6
Then using the following will update your table accordingly.
UPDATE `some_table`
SET `enq_id` =
IF(CHAR_LENGTH(`enq_id`) < 6,
LPAD(`enq_id`,6,'0'),
SUBSTRING(`enq_id`,1,6))
WHERE CHAR_LENGTH(`enq_id`) != 6
Note that the SUBSTRING() function deletes all the characters after the 6th character, and LPAD adds preceding zeros (in the above example) if needed, to each record.
UPDATE: For some reason I added an extra condition. The optimized code(s) should have been:
SELECT `enq_id`,LPAD(`enq_id`,6,'0') AS 'result'
FROM `some_table`
WHERE CHAR_LENGTH(`enq_id`) < 6
and
UPDATE `some_table`
SET `enq_id` = LPAD(`enq_id`,6,'0')
WHERE CHAR_LENGTH(`enq_id`) < 6