Multiple delete querie fail when 2/3 matches are found - mysql

Hi i am currently working with 3 tables where 1 querie delete all rows with matching id's, the problem is sometimes the third table t3 does not contain any data and therefore the whole script breaks.
Is there anything i can do to let this querie delete from the 2 out of 3 tables even tho there is no match in the third t3 table ?
DELETE t1, t2, t3
FROM table1 t1
table2 t2
table3 t3
WHERE t1.column1 = 1
AND t2.column2 = 1
AND t3.column3 = 1 // When this one does not have any matches the whole script fails
A simple but in my opinion 'weak' method of doing this is to split it up into 3 different queries but thats all a shame isent it?

Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
In your case, you want a LEFT JOIN:
DELETE t1, t2, t3
FROM table1 t1 LEFT JOIN
table2 t2
ON t1.column = t2.column LEFT JOIN
table3 t3
ON t1.column = t3.column
WHERE t1.column1 = 1;
This will work for missing rows in table2 and table3. If there are no matching rows in table1, then nothing will be deleted.

Related

How to update a table from another one depending on certain conditions and getting data from a third one if the conditions are not met?

I need to update a table with values coming from another table while statisfying a few conditions in the meantime.
To be more specific, the 'source' field of table1 needs to get updated by the field 'value' from table2.
These 2 tables share a common 'id' field that can be used for a join. If the 'id' of table1 doesn't have any correspondence in table2, then it should take the value of the 'source2' field from table3. If the 'source2' value is 'NULL', then it should be given a default value that we will name 'Default' in this example.
As an example, we have the table1 below, which is the one we want to update:
Then we have the table2 below, which is the source table that will be used to update table1:
Finally we have table3, which will be used if the information in table2 is unavailable:
Based on this example, I would like to write a query that would update table1 with the values below:
I have written the following query using MariaDB but obviously it is not correct:
UPDATE table1 T1
LEFT JOIN table2 T2 ON T1.id = T2.id
LEFT JOIN table3 a ON T1.id = T3.id
SET T1.source = if(T2.value is NULL, if(T3.source is NULL, 'Default', T3.source), T2.value)
WHERE T1.id = T2.id
Which parts should be amended to make it work?
You seem quite close. The left join logic is fine, we just need to adjust the conditional set and the where clause:
update table1 t1
left join table2 t2 on t2.id = t1.id
left join table3 t3 on t3.id = t1.id
set t1.source = coalesce(t2.value, t3.source, 'Default')
where t2.id is not null or t3.id is not null
coalesce() returns its first non-null argument.
The where clause ensures that we don't update rows that match in neither tables. You can remove it if you want to update all rows (those that do not match will get 'Default' assigned).

Delete rows via a join and a WHERE condition that uses a different table

I have 3 tables in a mysql database, I am wanting to delete from 2 of them via a JOIN based on the condition that a 3rd table doesn't have any ID's remaining that pertain to another record.
So far I have,
DELETE t1, t2
FROM t1
INNER JOIN t2 ON t1.qs_id = t2.qst_qs_id
What I can't work out is how to check the records of t3 to make a where clause work. The scenario I am trying to make work is as follows,
delete from t1 and t2, for any qs_ids that don't have any rows remaining in t3 table t3 has a column called qsa_qs_id, so if there are no rows matching the ids from the JOIN I can run the delete, but I cannot figure out how to put that into my WHERE.
PSEUDO would be,
DELETE from t1, t2, all rows that have matching qs_id columns BUT only do it if t3 doesn't have any rows with those qs_id remaining.
DELETE t1, t2
FROM t1
INNER JOIN t2 ON t1.qs_id = t2.qst_qs_id
WHERE t1.qs_id NOT IN (SELECT t3.qs_id FROM t3);

MySQL JOIN from one of the two table based on IF condition

SELECT Table1.Filter, Table1.Condition, Combined.Data FROM Table1
LEFT JOIN
(SELECT Key, Data FROM IF(Table1.Filter, Table2, Table3))) AS Combined
ON Table1.Condition = Combined.Key
I want to create a MySQL View that shows all columns of Table1, and a column from either Table2 or Table3 depending on the field on Table1.Filter.
One simple solution is to LEFT JOIN both Table2 and Table3, with NULL on the column that is not applicable. Is there a way to avoid creating 2 columns?
I cannot UNION Table2 and Table3 as they might contain the same Key.
The following should do what you want:
SELECT t1.Filter, t1.Condition,
COALESCE(t2.Data, t3.Data) as Data
FROM Table1 t1 LEFT JOIN
Table2 t2
ON t1.Filter AND t2.Key = t1.Condition LEFT JOIN
Table3 t3
ON (NOT t1.Filter) AND t3.key = t1.condition;
You cannot have conditionals choosing tables in the FROM. But, you can have conditions in the ON conditions.

Mysql - updating and insert using select * using a target column

update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.b = t2.b,
t1.c = t2.c;
This code works to join 2 tables on column a. My problem is that I have about 500 columns which I want to update and am currently writing out each of the 500 columns in the code up to
t1.500 = t2.500;
This works, but it is slow and inefficient. Does anyone know how you can select * from table2 to update table1, keeping the join on t1.a = t2.a? All of the column names match exactly and am inserting all of the columns from table2. Was thinking of something like this below although I know that this is not correct. Thank you!
update table1 t1
inner join
table2 t2 on
t1.a = t2.a
set t1.* = t2.*;
I think there is no way to make update query with a wildcard in mysql. Don't know if it will properly fit to your problem, but you can try this workaround. :
DELETE FROM table2 WHERE id IN (<ids>);
Delete all the records from table2 that are satisfying given condition. And then insert the corresponding records from table1 to table2.
INSERT INTO table2
SELECT * FROM table1 WHERE id IN (<ids>);

MySQL: How to Remove One Row of a Multi-Row Record Based on Column

If I have two tables that I'm joining and I write the most simple query possible like this:
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
There are a few records who have multiple rows per ID because they have multiple employers, so t1 looks like this:
ID Name Employer
12345 Jerry Comedy Cellar
12345 Jerry NBC
12348 Elaine Pendant Publishing
12346 George Real Estate
12346 George Yankees
12346 George NBC
12347 Kramer Kramerica Industries
t2 is linked with the similar IDs but with some activities that I'd like to see -- hence the SELECT * above. Though I don't want multiple rows to return if the Employer column is "NBC" -- but everything else is good.
The only other thing that matters here is that t2 is smaller than t1, because t1 is everybody and t2 are only from people who did particular activities -- so some of the matches won't return anything from t2, but I would still like them to be returned, hence the LEFT JOIN.
If I write the query like this:
SELECT *
FROM t1
LEFT JOIN t2 ON t1.id = t2.id
WHERE Employer <> "NBC"
Then it removes Jerry and George completely -- when really all I want is for the NBC row to not be returned, but to return any other rows that are associated with them.
How can I write the query while joining t1 with t2 to return each row except for the NBC ones? The ideal output would be all of the rows from t1 regardless if they match up with all of t2 except removing all of the rows with "NBC" as the employer in the return file. Basically the ideal here is to return the JOINs where they fit, but regardless remove the entire row for anybody with "NBC" as employer without removing their other rows.
The more I write about it, it seems like I should potentially just run a query prior to my JOIN to delete all the rows in t1 who have "NBC" as their employer and then run the normal query.
Basic subset filtering
You can filter either of the two merged (joined) subsets by extending the ON clause.
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer != 'NBC'
If you get null values now, and you don't want them, you'd add:
WHERE t2.Employer IS NOT NULL
extended logic:
SELECT *
FROM t1
LEFT JOIN t2
ON (t1.ID = t2.ID AND t2.Employer != 'NBC')
OR (t2.ID = t2.ID AND t2.Employer IS NULL)
Using UNION
Basically, JOIN is for horizontal linking and UNION does vertical linking of datasets.
It merges to resultsets: the first without NBC, and the second (which is basically an OUTER JOIN), adds everyone in t1 which is not part of t2.
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer != 'NBC'
UNION
SELECT *
FROM t1
LEFT JOIN t2
ON t1.ID = t2.ID
AND t2.Employer IS NULL
String manipulation in the resultset
If you just want to remove NBC as a string, here is a workaround:
SELECT
t1.*,
IF (t2.Employer = 'NBC', NULL, t2.Employer) AS Employer
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
This basically replaces "NBC" by NULL