I have written this delete query to remove the duplicate entries from table but when I run this query it will keep the old rows and delete the newest one. I want to keep the newest inserted rows and want to delete the old ones. I have appended ORDER BY clause in delete query but it's not working.
DELETE t2
FROM user_deprovision t1
JOIN user_deprovision t2 ON (
t2.UserName = t1.UserName AND
t2.GroupName = t1.Groupname AND
t2.id > t1.id)
ORDER BY t1.DeletedDate;
How can I achieve this.
Let's write a query that returns the latest time for a user name and group name.
SELECT
UserName, GroupName, MAX(deprovision_time)
FROM
user_deprovision
GROUP BY
UserName, GroupName
Now, let's adapt this to your purpose. In Oracle, I might do something to turn that into a temporary table, but I'll use correlated subqueries here instead.
DELETE FROM user_deprovision AS dp1
WHERE dp1.deprovision_time <
ANY (SELECT deprovision_time FROM user_deprovision AS dp2
WHERE dp1.UserName = dp2.UserName
AND dp1.GroupName = dp2.GroupName);
We can turn this into a join instead.
SELECT dp1.id
FROM user_deprovision AS dp1
JOIN user_deprovision AS dp2
ON dp1.UserName = dp2.UserName
AND dp1.GroupName = dp2.GroupName
WHERE
dp1.deprovision_time < dp2.deprovision_time
And the corresponding delete is:
DELETE FROM user_deprovision
WHERE id IN (
SELECT dp1.id
FROM user_deprovision AS dp1
JOIN user_deprovision AS dp2
ON dp1.UserName = dp2.UserName
AND dp1.GroupName = dp2.GroupName
WHERE
dp1.deprovision_time < dp2.deprovision_time);
Related
hello i am trying to search for duplicates in a table within a period using this snippet.
FROM
table WHERE Start_Date BETWEEN '2018-07-01' AND '2018-07-31'
GROUP BY Policy_Number
HAVING COUNT(Policy_Number) > 1;
this produces all the duplicates records in the table within the required dates with their counts.
Now i am trying to delete those duplicate records using this snippets using this snippet i have also found online
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND Start_Date BETWEEN '2018-07-01' AND '2018-07-31';
but i keep getting this error
Column 'Start_Date' in where clause is ambiguous
Please how can i correct this to delete the duplicates i want removed thanks!!
write like this way t1.Start_Date its work
Try running as a query previous executing your select:
SET SQL_BIG_SELECTS=1;
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND t1.Start_Date BETWEEN '2018-07-01' AND '2018-07-31';
Because you create t1 and t2 using same table table so both have start_date and thats why its give Column 'Start_Date' in where clause is ambiguous error
Define the alias before start_Date
SET OPTION SQL_BIG_SELECTS = 1
DELETE t1 FROM table t1
INNER JOIN
table t2
WHERE
t1.id < t2.id AND t1.Policy_Number = t2.Policy_Number AND t1.Start_Date BETWEEN '2018-07-01' AND '2018-07-31'
an example works
SELECT
table.Number
FROM table
INNER JOIN table2
ON table.ID = table2.ID
WHERE checkInDate BETWEEN '2015-09-12' AND '2015-09-13';
I am using a command that runs every second on node.js It has the function of excluding any duplicate records for a given item, which is specified as in the example by: AND t1.auction_id = 1335.
DELETE FROM bid_account t1
WHERE t1.id < (Select max(t1.id) FROM bid_account t2 WHERE t1.bidding_price = t2.bidding_price) AND t1.auction_id = 1335;
I need it to delete a record that has an equal value in the bidding_price column, and keep only one. But it is important that he does this search not across the table, but rather for a certain item as I reported at the beginning, through the column auction_id.
I tried to run the above command, but it returns the following error:
#1064 - You have a syntax error in your SQL next to 't1
WHERE t1.id < (Select max(t1.id) FROM bid_account t2 WHERE t1.bidding_price ' na linha 1
What is wrong with this query?
I use the MYSQL database, and the table bid_account has the id column as index and primary.
If I use SELECT below, it returns the values in duplicity normally.
SELECT bidding_price, count(*) FROM bid_account WHERE `auction_id` = 1335 GROUP BY bidding_price Having Count(*) > 1
You have two quirks from MySQL. First, when you use an alias for a table, it needs to follow the DELETE. So, this is what you intend:
DELETE ba FROM bid_account ba
WHERE ba.id < (Select max(ba2.id) -- I assume you want the max from the innter reference
FROM bid_account ba2
WHERE ba2.bidding_price = ba.bidding_price
) AND
ba.auction_id = 1335;
However, this still will not work, because you are referencing bid_account in the table.
So, I think you want something more like this:
DELETE ba
FROM bid_account ba JOIN
(SELECT ba2.auction_id, ba2.bidding_price, MAX(ba2.id) as max_id -- I assume you want the max from the innter reference
FROM bid_account ba2
WHERE ba2.auction_id = 1335
GROUP BY ba2.auction_id, ba2.bidding_price
) ba2
ON ba2.auction_id = ba.ba2.auction_id AND
ba2.bidding_price = ba.bidding_price AND
ba2.max_id > ba.id
WHERE ba.auction_id = 1335;
This still doesn't strike me as useful. Matching rows based on bidding_price seems unusual. I would expect auction_id to be part of the match. I suspect you want to GROUP BY and JOIN on both bidding_price and auction_id.
You can delete row by using alias before from keyword in sql query
for example
delete t from test t where t.id = 3
So your query will work fine
DELETE t1 FROM bid_account t1
WHERE t1.id < (Select max(t1.id) FROM bid_account t2 WHERE t1.bidding_price = t2.bidding_price) AND t1.auction_id = 1335;
In a MySQL database I have a many-to-many relationship between two tables. For the sake of simplicity let's assume those tables map homes and their residents. I have a third table to map those relations (home_resident_relations). The latter table has an additional column datemodified that stores the date of the latest update of each row via triggers.
Now I want to get rid of all former residents for each home and only keep the current ones - that is those with the newest date.
I have already a working SELECT clause that will list me all old relations I want to delete:
SELECT * FROM `home_resident_relations` WHERE `resident_id` NOT IN
(SELECT tbl.`resident_id`
FROM `home_resident_relations` tbl
WHERE tbl.`datemodified` =
(SELECT max(tbl2.`datemodified`)
FROM `home_resident_relations` tbl2
WHERE tbl2.`home` = tbl.`home`
GROUP BY tbl2.`home`)
OR tbl.`datemodified` IS NULL
);
Now it would be a straight-forward idea to simply replace the SELECT * with a DELETE command to remove all those rows. However, this does not work due to error
#1093 - You can't specify target table 'home_resident_relations' for update in FROM clause
So here's my question:
How do I delete from a table while using it in the WHERE ... NOT IN clause?
Use a left join instead:
DELETE hrr
FROM `home_resident_relations` hrr LEFT JOIN
(SELECT tbl.`resident_id`
FROM `home_resident_relations` tbl
WHERE tbl.`datemodified` = (SELECT max(tbl2.`datemodified`)
FROM `home_resident_relations` tbl2
WHERE tbl2.`home` = tbl.`home`
GROUP BY tbl2.`home`
) OR
tbl.`datemodified` IS NULL
) tt
ON hrd.resident_id = tt.resident_id
WHERE tt.resident_id IS NULL;
This works for both the SELECT and DELETE.
Try using DELETE with join:
DELETE FROM `home_resident_relations`
LEFT OUTER JOIN
(SELECT tbl.`resident_id`
FROM `home_resident_relations` tbl
WHERE tbl.`datemodified` =
(SELECT max(tbl2.`datemodified`)
FROM `home_resident_relations` tbl2
WHERE tbl2.`home` = tbl.`home` )
OR tbl.`datemodified` IS NULL) s
ON(s.`resident_id` = `home_resident_relations`.`resident_id`)
WHERE s.`resident_id` is null
I have the query below that shows me duplicates in my table. I would like to know how can i turn this into a delete query to delete these duplicate rows but leaving just one. My table does have a auto increment id column.
SELECT * FROM tbl_user_tmp AS t1
INNER JOIN (
SELECT name, activity, class, COUNT(1) AS cnt FROM tbl_user_tmp
WHERE user = 'test' AND disregard = 0
GROUP BY name, activity, class
HAVING cnt > 1
) AS t2
ON t1.name = t2.name AND t1.activity = t2.activity AND t1.class = t2.class
WHERE user = 'test' AND disregard = 0
GROUP BY t1.name, t1.activity, t1.class
I have tried the query below and seems to work, but im afraid im missing something. does it look correct?
delete from tbl_user_tmp
where user='test' AND id not in
(
select minid from
(select min(id) as minid from tbl_user_tmp where user='test' group by name, activity, class) as newtable
)
You can use LIMIT.
Example:
DELETE FROM users
LIMIT 2;
Now you just need to set COUNT - 1 as your limit ;)
I have two different tables
in first one (t1) I have
Table [t1]
id Product_URL
.
Table [t2]
id Product_id Product_URL
I would like to UPDATE ALL product_id field (from t2) to the id of the first
WHERE t1.product_url = t2.product_url
can I do that In one query?
UPDATE t2
JOIN (
SELECT t2_2.id, t1.id as new_id
FROM
t2 t2_2 JOIN
t1 ON t2_2.product_url = t1.product_url AND t2_2.product_id <> t1.id
ORDER BY t2_2.id
LIMIT 5000
) sub ON t2.id = sub.id
SET id = sub.new_id;
EDIT: It looks like mutli-table updates do not play well with LIMIT and ORDER BY, but here's another query that accomplishes the same thing...