Right now I am using something like this to delete duplicates in mysql table :
delete t2 from my_table1 as t1, my_table1 as t2 where
t1.TestCase = t2.TestCase and t2.id > t1.id;
say I have a structure like this :
ID TestCAse Result
1 T1 PASS
2 T2 FAIL
3 T3 FAIL
4 T3 PASS
now, in the above case T3 is duplicate entry, and if I use the SQL that I mentioned above, it would delete 4th row where the result is PASS, but this is the row that I want to keep and I want row 3 to get deleted which is FAIL.
Any help please?
Thank you.
if I undersstand correctly in case of duplicate you want to delete the "FAIL" and not the "PASS" ? in this case you can have the following query:
delete t2 from my_table1 as t1, my_table1 as t2 where
t1.TestCase = t2.TestCase and t2.id != t1.id and t2.Result='FAIL';
but what do you want to do when all the duplicate have "FAIL" in their column result? With the query above, both will be removed. Do you want to keep one in this case ?
Related
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).
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.
I have a pretty simple MySQL query to implement, but I can't figure out how...
I have two tables, T1 and T2.
What I need to do:
From T1, I retrieve an ID based on a CODE value:
SELECT id FROM T1 WHERE code = '$code';
Then I need to use this ID (so the value I just retrieved) to update a specific row in T2 (the name of the row will match the ID's value).
I was thinking about using either subqueries or user-defined variables, but no matter how I try it I can't get it done.
If you have any code snippet that can help me doing that, I would appreciate it as well!
EDIT
Just to clarify something: I don't know the name of the column that I need to update in T2, since that name will be the value I retrieve from T1.
So for example, if the ID I get from T1 is "03", it will update the column named "03" in T2.
EDIT 2
Here's a little schema of what I intend to achieve (hoping I make myself clearer, I'm sorry for the misunderstanding...)
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID = (SELECT id FROM T1 WHERE code = '$code')
UPDATE: If the sub query returns more than one row then you can use from IN operator
UPDATE T2 SET COL = YOUR_VALUE
WHERE T2.ID IN (SELECT id FROM T1 WHERE code = '$code')
Use an UPDATE with a JOIN:
UPDATE T2
CROSS JOIN T1
SET T2.`0` = IF(T1.id = 0, T1.someColumn, T2.`0`),
T2.`1` = IF(T1.id = 1, T1.someColumn, T2.`1`),
T2.`2` = IF(T1.id = 1, T1.someColumn, T2.`1`)
WHERE T1.code = '$code'
Replace someColumn with the column in T1 containing the value you want to put into T2.
you can update without using the subquery just using join
update t2
inner join t1 on t2.name = t1.id and t1.code ='$code'
set t2.my_col = 'my_value'
but you should not use var in your query you are at risk for sql injection take a look at you mysql driver for param_binding
UPDATE T2 SET COL = YOUR_VALUE WHERE EXISTS
(SELECT 1 FROM T1 WHERE T2.id=T2.id AND code = '$code')
You can update T2 with the next SQL if you expect at least one row from the inner query
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID IN (SELECT ID FROM T1 WHERE CODE = $code)
Otherwise, if from T1 you are sure you will get only 1 record
UPDATE T2 SET COLUMN = VALUE
WHERE T2.ID = (SELECT ID FROM T1 WHERE CODE = $code)
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>);
I have the following problem.
I have a table which contains the following fields (example):
id, id2, id3, id4
Due to a mistake, I have accidently deleted all values of id3. (They are NULL now).
I have a second file (backup) which is a bit older, so it doesn't have the same count as the damaged table. The id3 is present there.
How do I "join" these tables, to restore at least the bigger part? Insert id3 ONLY to the damaged table from the non-damaged table?
I tried this:
UPDATE table1 SET `id3` = SELECT `id3` FROM table2
In my case, only phpMyAdmin or SQL Syntax solution (no console) would work...
Please help!
If we assume that one, two, or three of the ids define each row, then you can use join:
update table1 t1 join
table2 t2
on t1.id = t2.id
set t1.id3 = t2.id3;
This assumes that id is unique in the two tables. You might want to use a more complex expression:
update table1 t1 join
table2 t2
on t1.id = t2.id and t1.id2 = t2.id2 and t1.id4 = t2.id4
set t1.id3 = t2.id3;
Assuming id is an unique attribute.
You can simply run this query:
UPDATE table1 SET id3 = (SELECT id3 FROM table2 WHERE table2.id = table1.id)