I have the following select query
SELECT t1.*
FROM t1, t2
WHERE t1.field1=t2.field1 And t1.field2=t2.field2 And t1.field3=t2.field3 ;
I want to convert this into a delete query. how should i write it?
What about this query:
DELETE FROM t1
WHERE t1.field1 IN (
SELECT t1.field1 FROM t1, t2
WHERE t1.field1=t2.field1 And
t1.field2=t2.field2 And
t1.field3=t2.field3)
Not 100% on access but does:
DELETE t1
FROM t1, t2
WHERE t1.field1=t2.field1 And t1.field2=t2.field2 And t1.field3=t2.field3 ;
Work?
Try this
DELETE FROM t1
FROM t1 AS tt1, t2 AS tt2
WHERE tt1.field1=tt2.field1 And tt1.field2=tt2.field2 And tt1.field3=tt2.field3 ;
EDIT:
Did this in MS Access
DELETE DISTINCTROW t1.*
FROM t1 INNER JOIN t2 ON (t1.field3 = t2.field3) AND (t1.field2 = t2.field2) AND (t1.field1 = t2.field1);
And it worked, you have to set the Unique Records to Yes
What about this:
DELETE
FROM t1
INNER JOIN t2 ON t1.field1=t2.field1
And t1.field2=t2.field2
And t1.field3=t2.field3
Will delete all records in t1 that have a matching record in t2 based on the three field values.
I've been struggling with something similar.
I found the easiest way is not to use a query at all, but to create an empty duplicate table with multiple Primary Keys with Duplicates OK set (Design View hold down Ctrl key and select the rows you want and then right click and select them all as Primary Keys).
Then copy and paste all the rows from your table into the new table. OK the error messages and you will find you have a table with only unique values in the fields you wanted.
That has the additional benefit of not allowing duplicate rows in your new table.
Related
I want to make a query that deletes duplicate data leaving only one duplicate data when two columns overlap.
Maybe because of a lot of data, but the following query doesn't work for a long time
DELETE t1 FROM table t1 INNER JOIN table t2
WHERE t1.idx < t2.idx AND t1.Nm = t2.Nm AND t1.product = t2.product;
Can this query do what I want? If not, what is the other way?
Create an Index on the 3 columns involved in the ON clause:
CREATE INDEX idx_name
ON tablename (Nm, product, idx);
and execute the query like this:
DELETE t1 FROM tablename t1 INNER JOIN tablename t2
WHERE t1.Nm = t2.Nm AND t1.product = t2.product AND t1.idx < t2.idx;
As you can see in this simplified demo, the query will be executed using the index.
I have got a database with about 7000 cars, but unfortunately, only about 1000 are unique. Can I delete all the duplicated rows?
The schema looks like this:
Thank you!
Here is one way to do it:
delete t1
from mytable t1
inner join mytable t2
on t2.brand = t1.brand
and t2.model = t1.model
and t2.id < t1.id
This will delete duplicates on (brand, model) while retaining the one with the smallest id.
I have two tables t1, t2 that I have created and loaded data from a CSV into these.
I had to then create a new PK column as the existing columns (t1.old_id, t2.old_id) are strings that would naturally be a PK are not absolutely fixed (this seems to be advised against?)
so I created a id PK INT AUTO_INCREMENT in each table
as one record in t1 is linked to many in t2 and I want to maintain referential integrity between these two tables.
I believe what i need to do is create an id INT NOT NULL in t2 as an FK
This t2.id is blank at the moment (as it is dependent ont1.id`)
Am I right in thinking I need an UPDATE query with a JOIN of some description to make this work?
The following produces the data exactly that I want to update into my t2.id column - but I don't know how to do the update
select t1.id
from t1
inner join t2
on t1.old_id = t2.old_id
You can use a join in your UPDATE statement like this:
UPDATE t2
JOIN t1 ON t1.old_id = t2.old_id
SET t2.id = t1.id
You can use a correlated UPDATE query like this
UPDATE t2
SET id = (SELECT MAX(t1.id) FROM t1 WHERE t1.old_id = t2.old_id);
*Assuming you have a single t1.id for each t1.old_id
On a Separate Note, You should name t2.id like t2.t1ID so as to remove ambiguity if and when you have a identity column in t2 as well named id
This must be very trivial but I can't seem to find the solution.
I work with two tables, both without any primary key.
I want to add all the records of the first table to the second table only if they don't exist.
Basically:
INSERT INTO Table2
SELECT Table1.*
FROM Table
WHERE "the record to be added doesn't already exists in Table2"
You could do something like this. You would need to check each relevant column - I have just put in 2 as an example. With a Not Exists clause you can check if a record already existed across multiple columns. With a NOT IN you would only be able to check if a record already existed against one column.
INSERT INTO Table2
SELECT t1.*
FROM Table1 t1
WHERE NOT EXISTS
(
SELECT 1
FROM table2 t2 WHERE
t2.col1 = t1.col1 AND
t2.col2 = t1.col2
)
you could make usage of the EXISTS function:
INSERT INTO Table2
SELECT Table1.*
FROM Table1
WHERE NOT EXISTS(SELECT * FROM table2 WHERE <your expression to compare the two tables goes here>)
But i would advise you to check the use of unique index for your tables
Just an idea - untested:
INSERT INTO Table2
SELECT *
FROM Table1
WHERE NOT EXISTS(SELECT * FROM Table2 WHERE Table2.Field1 = Table1.Field1 AND Table2.Field2 = Table1.Field2)
You must add every Field of both Tables in the WHERE clause of the NOT EXISTS Query
INSERT INTO X.TableX1
(ColumX1,ColumnX2)
SELECT DISTINCT c.[ColumnY1],c.[ColumnY2]
FROM Y.Table2 c INNER JOIN Database.z.Table3 i ON c.ColumnX1= i.ColumnY1
WHERE NOT EXISTS
(
SELECT *
FROM X.TableX1 WHERE
X.TableX1.ColumnX1=c.ColumnY1
)
This is Joining two tables and filtering the data required and updating only the new values to third table on every run
I am trying to find all the records that are in t1 but not in t2. I know there are more records in t1 than in t2 because when I run
select count(*)
from t1;
select count(*)
from t2;
I get 21,500 records and 21,000 records respectively. But the problem is these tables are not normalized, there are no primary keys, therefore I cannot do something like this:
SELECT id FROM t1
where t1.id
not in (
SELECT t2.id
FROM t2
where t2.id is not null);
or this
SELECT t1.id, t2.id
FROM t1
LEFT JOIN t2
ON t1.id = t2.id
where t2.id is null
as both return null, as the id numbers match perfectly, there seems to be the same exact amount of ids. There must be another field which is not matching.
UPDATE
I ended up doing this:
select id, count(id)
from t1
group by id;
select id, count(id)
from t2
group by id
it gave the same amount of claim numbers and the count of times it shows up. I copied and pasted it into excel and just subtracted one count from the other and did a conditional formatting to only show the ones that are not zero and this gave me all the ids that showed up in one table more than the other. (Sloppy solution, but it was able to resolve the issue).
You have two problems. A bad database design and somehow bogus data is being inserted into your tables.
I don't know if this will work without indexes.
A left outer join should get you started (look up the syntax).
You should end up with something like:
t1.id t2.id
1 1
2 2
3 null
4 4
Try to fix the tables by adding a primary key on 'id' to both tables using MySQL:
ALTER TABLE t1 ADD PRIMARY KEY (id)
ALTER TABLE t2 ADD PRIMARY KEY (id)