How does this Update Statement work? - mysql

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)

Related

Mysql sentence update one column on multiple WHERE

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);

how to set pick value from database in variable using Google apps script

MySQL table1 structure:
id name age status
1 roy 30 1
2 jon 40 0
3 roky 25 1
4 jerin 28 1
number_row=select * FROM table1 where status=0
for{i=0;i<number_row;i++}
if (status==0){
update that row and save in table2
}else{
no update
}
}
Code: I can't set pick value from database table in variable to apply
If Else condition.
Change
if (status=0){
to
if (status==0){
= is assignment
== is equality
=== is strict equality

delete with alias and two conditions only delete one row

I am trying delete all relations like these
SELECT *
FROM wr_entidadUsuario a
WHERE a.entidad_id IS NOT NULL
AND a.usuario_id IN (117)
AND a.entidad_id IN (330,429,425,427,428
,308, 10,313,400,357
,406,416,356, 46,314
,104,326,327,328,361
,351,360,399,282,310)
22 row(s) returned
but when i try to delete these rows only 1 is affected
DELETE a
FROM wr_entidadUsuario a
WHERE a.entidad_id IS NOT NULL
AND a.usuario_id IN (117)
AND a.entidad_id IN (330,429,425,427,428
,308, 10,313,400,357
,406,416,356, 46,314
,104,326,327,328,361
,351,360,399,282,310)
1 row(s) affected
what is the problem?
I am using mysql

Mysql REPLACE resulted in 0 values on all rows

I've used the following SQL query in a MySQL database to replace part of a string in a cell:
UPDATE TEST.database2014 SET together1 = REPLACE(together1, "/1900", "20")
For some reason all the rows (225,000!) have now a value of 0.
This is the message which I got:
/* Affected rows:225,000 Found rows: 0 Warnings: 0 Duration for 1 query: 16,888 sec. */
ADDITIONAL INFORMATION:
data example contained in field together1:
TESTING^^^19/01/2014^^
Is there a known reason for this happening?
I find it strange that if no matches where found it converted all values to 0 anyway.
I think that you must use this:
UPDATE TEST.database2014 SET together1 = REPLACE(together1, "/19", "/20") WHERE togheter1 LIKE '%/19%'
if you want to upate all year 1900 to 2000

Can't update table in MySQL, I am using the workbench

I am a fairly new user to MySQL although I am fairly experienced with SQL (DB2 environment). I am using the workbench to run queries and update statements. I am having a problem updating data in a table which I have been able to prior. I'm able to select rows but when I go to update based on the same criteria, the return message is:
**0 row(s) affected Rows matched: 9 Changed: 0 Warnings: 0**
Update gina1.proj001_bcbs set contract_percentage = 1.50
where contract_category = 'All Other Services'
and doctor = 'JSmith' ;
When I run the same WHERE clause with a select I get the correct list of records.
**9 row(s) returned** and I get the correct list of data.
select * from gina1.proj001_bcbs
where contract_category = 'All Other Services'
and doctor = 'JSmith' ;
I do not believe I am logging but I can't say for sure, I did ready somewhere about resetting the log. If someone can help it would be great.
This means simply, that all relevant records already have contract_percentage = 1.50
0 row(s) affected : No rows were affected by your query
Rows matched: 9 : 9 rows were found, ...
Changed: 0 : ... but none of them had to be changed
Warnings: 0 : Nothing recoverable bad happened while runinng the query
.
Update gina1.proj001_bcbs set contract_percentage = 2.50
where contract_category = 'All Other Services' and doctor = 'JSmith' ;
Is ver likely to bring you 9 row(s) affected Rows matched: 9 Changed: 9 Warnings: 0