delete with alias and two conditions only delete one row - mysql

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

Related

MySQL query with a Max row value with multiple conditions

I have a table like this
gen_loc
rev_number
status
action
5A2
09
PROCESSED
INSERT
5A2
10
PROCESSED
INSERT
5A2
10
PROCESSED
DELETE
8A5
09
PROCESSED
INSERT
8A5
10
PROCESSED
UPDATE
10A6
09
PROCESSED
INSERT
10A6
10
PROCESSED
DELETE
I am trying to select the rows which contains the MAX value from rev_number column BUT ONLY IF THIS COLUMN ITS THE MAX VALUE WITH ACTION DIFFERENT TO DELETE AND EQUAL TO PROCESSED
As you can see, in the gen_loc 10A6 we have 10 as a max value but action delete so, I expect that the query doesn't show the gen_loc 10A6 for this case.
I have been trying other queries using MAX(rev_number) but it shows me still the gen_loc with 09 rev_number.
I expect this result:
gen_loc
rev_number
status
action
5A2
10
PROCESSED
INSERT
8A5
10
PROCESSED
UPDATE
How to select a Max value with the condition that the action IS NOT DELETE AND STATUS EQUAL TO PROCESSED?
Thank you!
Check out this db fiddle
If you don't want to group by any column, you can do in this way
SELECT tbl.*
FROM tbl
JOIN
(
SELECT max(rev_number) as t2_revNum
FROM tbl
) t2 ON tbl.rev_number=t2.t2_revNum
WHERE status = 'PROCESSED' AND action <> 'Delete'

MySQL / HeidiSQL - affected rows in log is 0 but rows have definitely been affected

I'm updating a a large number of rows (~168k) with an UPDATE query that contains various JOINS.
(i changed the field and table names, but the logic of the query stays the same)
UPDATE
table_a
JOIN table_b on table_a.field1 = table_b=field1
JOIN table_c on table_b.field2 = table_c.field2
JOIN table_d on table_c.field3 = table_d.field3
JOIN table_e on table_d.field4 = table_e.field4
SET
table_a.date_field = DATE_ADD(table_a.date_field, INTERVAL 12 MONTH)
WHERE
table_a.checkfield IS NOT NULL
AND table_a.date_field >= "2017-07-05"
AND table_e.statefield != "DEU"
AND
(
table_c.statusfield1 IN (2,3,9) OR (table_c.statusfield1 = 1 AND table_c.statusfield2 = 1 AND table_c.statusfield3 IS NULL)
)
I'm a 100% sure this update query affected about 167k rows and checked it already in the database, but the mysql query log in HeidiSQL 9.5.0.5196 said:
"/* Affected rows: 0 Found rows: 0 Warnings: 0 Duration for 1 query: 52,984 sec. */"
I already ran this query limited to only 4 or 5 items and there the affected row count was correct, but if i run the whole update query the row count is alway zero.
Is this a known bug in HeidiSQL or my Percona/MySQL Version?
The system:
Server version: 5.6.40-84.0 - Percona Server (GPL), Release 84.0, Revision 47234b3, Server OS: debian-linux-gnu, HeidiSQL Version:9.5.0.5196

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)

MS Access delete Query

I have query in MS Access that produces a correct records result, but Access refuses to run the Query as a delete query?
Can any one help me rewrite this query to run in access.
Delete Table_A.*
FROM (SELECT Table_A.Main_RecID, Table_A.Fld_Unique_ID, Table_A.Actiontaken FROM Table_A
WHERE Table_A.Actiontaken="MainRecord deleted") AS Tmp_B
LEFT JOIN Table_A ON Tmp_B.Main_RecID=Table_A.Main_Recid
WHERE (((Table_A.Actiontaken)<>"MainRecord deleted"));
If the "Delete" is replaced by a select or I ask for a datasheet view the Query produces what I would expect. Which is a list of the records in the table that have the same Main_RecID as records with Actiontaken field = "MainRecord deleted" but do not have their Actiontaken field equal to "MainRecord deleted".
Access responds with the message "Could not delete from specified tables."
I started with this data in Table_A ...
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
2 1
3 2 MainRecord deleted
4 2 something else
5 3 something else
Note Actiontaken is Null in second row (where Fld_Unique_ID = 2).
Executing the DELETE statement below leaves this data in the table.
Fld_Unique_ID Main_RecID Actiontaken
1 1 MainRecord deleted
3 2 MainRecord deleted
5 3 something else
DELETE
FROM Table_A
WHERE
(
Actiontaken<>'MainRecord deleted'
OR Actiontaken Is Null
)
AND
(
DCount("*", "Table_A",
"Main_RecID = " & Main_RecID
& " AND Actiontaken='MainRecord deleted'") > 0
);
If that's not what you're after, please show us sample data before and after DELETE.

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