Mysql sentence update one column on multiple WHERE - mysql

I need to do this.
UPDATE tx_seminars_attendances SET user=941 WHERE uid=791;
UPDATE tx_seminars_attendances SET user=54 WHERE uid=3439;
UPDATE tx_seminars_attendances SET user=453 WHERE uid=945;
UPDATE tx_seminars_attendances SET user=1421 WHERE uid=3219;
UPDATE tx_seminars_attendances SET user=33 WHERE uid=943;
I need some kind of conditioning right?

You can use the in operator in the where clause and then make the distinction using a case expression:
UPDATE tx_seminars_attendances
SET user = CASE uid
WHEN 791 THEN 941
WHEN 3439 THEN 54
WHEN 945 THEN 453
WHEN 3219 THEN 1421
WHEN 943 THEN 33
END
WHERE uid in (781, 3439, 945, 3219, 943);

Related

MySQL replace function not specific enough

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

MySql update Query For Multiple rows Update

I have value in column seq_id = {18,19,20,21,22,23,24}
I want to update this by {19,20,21,22,23,24,18}
I want to update this column only .other columns remain as it is.
How can i update this in mysql query.
Try this:
UPDATE yourTable
SET seq_id = seq_id + 1
WHERE seq_id BETWEEN 18 AND 23
UPDATE yourTable
SET seq_id = 18
WHERE seq_id = 24

Which SQL command should I use to replace specific values?

I have numeric values like below:
0
2323
1003
I have to replace only single zeros to 100. I tried the following code but it affects the other values which include 0.
UPDATE table_name SET field_name= replace(field_name,"0","100");
The values should be like below after the transaction:
100
2323
1003
Which SQL command should I use?
Thanks.
You should use a where clause instead of replace.
UPDATE table_name SET field_name = 100
where field_name = 0

Update query for 110 million records won't finish

We are using MySQL.
We have a very long table (110m * 7) and we regularly add in new records to this table (when doing that, we upload csv files so never a really painful process even large # of records).
Now we need to add a new column to this table to distinguish the records in some way. To be specific, the most recent added about 10 million records will be flagged as type 2 and all the old records will be flagged as type 1. And in the future, we will be putting in new records for both types.
At first, we tried the approach 1 as below, but it ran for more than 24 hours without complaining anything and the host server has always been responsive.
-- ====================================================
-- start of approach 1
-- ====================================================
-- Add column.
ALTER TABLE
bond_price
ADD
bp_name_version SMALLINT
;
-- Set value 1 for BOND_CHARACTER types.
UPDATE
bond_price
SET
bp_name_version = 1
WHERE
bp_serial_id < 107480325
;
-- Set value 2 for BOND_CHARACTER_EIKON types.
UPDATE
bond_price
SET
bp_name_version = 2
WHERE
bp_serial_id >= 107480325
;
-- Set a NOT NULL constrain on the new column
ALTER TABLE
bond_price
ALTER
bp_name_version SET NOT NULL
;
-- ====================================================
-- END of approach 1
-- ====================================================
We lost our faith after 24 hours, thinking setting a conditional clause might has made it difficult for such a long table. So we tried approach 2 which is doing approach 1 step by step and without conditional clause.
So we firstly execute the following query and it finished within seconds.
-- Add column.
ALTER TABLE
bond_price
ADD
bp_name_version SMALLINT
;
We then execute the following query to put value 1 to all records, hoping later on we can change the value for only 10 million records to 2.
-- Set value 1 for all records.
UPDATE
bond_price
SET
bp_name_version = 1;
But this query has run for over 24 hours till now, again without complaining for anything.
We have been monitoring the server by:
select * from pg_stat_activity;
And the 'Set value 1' query is still active and the server is still very responsive.
OUR QUESTIONS:
Is this speed what one should expect, considering the record number is over 100 million?
Is there a possibility that this query will not work but just get stuck forever? Is there anyway to tell?
Is there anyway to improve the speed? Or do it a different way?
Many thanks in advance!
I don't know the basis of the data, but 110+ million records in one push is probably a bit nuts.
Why not trying to find some other criteria, do a loop, and do them in smaller chunks... maybe based on some "Add" Date, or some other field in the structure. Or, just use the bp_serial_id, such as (again, handled by a loop)
pseudo-code to update 1 million max at a time
maxSerialForType1 = 107480325
for cycle = 0 to 110
startSerialID = cycle * 1000000
endSerialID = (cycle +1 ) * 1000000
if startSerialID < maxSerialForType1
UPDATE bond_price
SET bp_name_version = 1
WHERE bp_serial_id < maxSerialForType1
AND bp_serial_id >= startSerialID
AND bp_serial_id < endSerialID
end for update type 1
if startSerialID > maxSerialForType1
UPDATE bond_price
SET bp_name_version = 2
WHERE bp_serial_id > maxSerialForType1
AND bp_serial_id >= startSerialID
AND bp_serial_id < endSerialID
end for update type 2
end of loop
So this, if made like a stored procedure will update the 110 million for you.
I would then make a suggestion, that when importing new records into a temp table with the bp_name_version value assigned there, THEN pull them into the final table so you don't have to go through trying to update 110+ million each time.

How does this Update Statement work?

I had a coworker today write a improperly written SQL update.
UPDATE table SET column = 'change'
WHERE id = 2401 OR 2402 OR 2403 OR 2404 OR 2405 OR 2406;
Query OK, 264 rows affected (0.03 sec)
Rows matched: 9997 Changed: 264 Warnings: 0
Why would this work?
I thought at most it would update 1 row with ID being unique. and 2402 and 2403....etc not being matched against anything.
What happened is OR 2402 was treated as a true value, as if you put 1 = 1 or another condition that always returns true. Your query was read by MySQL as
UPDATE table SET column = 'change'
WHERE id = 2401 OR true OR true OR true OR true OR true;
That statement is wrong and would update all your records. The
correct query is:
UPDATE table
SET column = 'change'
WHERE id = 2401 OR id= 2402 OR id=2403 OR id=2404 OR id=2405 OR id=2406;
or better:
UPDATE table
SET column = 'change'
WHERE id IN (2401,2402,2403,2404,2405,2406);
your original query updates record whose id is 2401 and any other row for which holds that 2042 is "true" which it is for all (as 2042 isn't 0 or false)