I have to check wether two queries (e.g. an update and a delete statement) affect the same row in a MySQL database.
I thought about selecting the affected entries of the queries with a select and check if the same row appears in both queries:
UPDATE Foo SET col = 'a' WHERE id > 5;
DELETE FROM Foo WHERE col = 'b';
SELECT (SELECT * FROM Foo WHERE col = 'b') IN (SELECT * FROM Foo WHERE id > 5);
In practice it worked for my simple Foo table, but failed in another table with several columns – even when the two sub-selects where exactly the same. Also with this solution the first sub-select can only contain one result.
Does anyone know another solution or the reason why the above select could fail with another table?
I solved my problem partly with PHP now, instead of solving it only with MySQL.
I select the affected entries of the concerned queries and compare the result in my application (in my case it’s realised with PHP).
Why don’t I solved it via one query over MySQL?
At the moment were I’ve to compare the queries, I don’t know anything else given by the query. Still I know which tables are affected and which columns, but I don’t know how this tables are structured or which column is a primary key (or if there are even multiple primary keys). That’s why I, more or less, wrote my own IN function in PHP.
Another way of solution could be to get the primary keys from MySQL (e.g. using DESCRIBE) and using the described solution of Joachim Isaksson.
Just wanted to share this with you.