I'm having trouble writing a query for the table below.
CREATE TABLE `c_example` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`c_id` int(11) DEFAULT NULL,
`guid` varchar(32) DEFAULT NULL,
`provider_id` int(11) DEFAULT NULL,
`test_timestamp` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
) ENGINE=MyISAM AUTO_INCREMENT=5731 DEFAULT CHARSET=utf8;
In this table the data will look like
1,1,313f516ad35e477482aa435ae53c6b3a,1,2016-05-09 22:53:14
2,1,313f516ad35e477482aa435ae53c6b3a,2,2016-05-09 22:53:14
3,1,313f516ad35e477482aa435ae53c6b3a,3,2016-05-09 22:53:14
4,1,f76d2c14a86a42378badd04e4051e569,1,2016-05-10 22:53:14
5,1,f76d2c14a86a42378badd04e4051e569,2,2016-05-10 22:53:14
6,1,f76d2c14a86a42378badd04e4051e569,3,2016-05-10 22:53:14
7,2,c040cecfbff648b2a3ecbd9d0688efec,1,2016-05-11 22:53:14
8,2,c040cecfbff648b2a3ecbd9d0688efec,2,2016-05-11 22:53:14
9,2,c040cecfbff648b2a3ecbd9d0688efec,3,2016-05-11 22:53:14
10,2,79c87ba31df14ec89f0da39e98b6a088,1,2016-05-12 22:53:14
11,2,79c87ba31df14ec89f0da39e98b6a088,2,2016-05-12 22:53:14
12,2,79c87ba31df14ec89f0da39e98b6a088,3,2016-05-12 22:53:14
Now, I want to delete all the rows where the unique combination of c_id, guid that has a count > 1 and delete the ones with the oldest timestamp, so I'm left with.
4,1,f76d2c14a86a42378badd04e4051e569,1,2016-05-10 22:53:14
5,1,f76d2c14a86a42378badd04e4051e569,2,2016-05-10 22:53:14
6,1,f76d2c14a86a42378badd04e4051e569,3,2016-05-10 22:53:14
10,2,79c87ba31df14ec89f0da39e98b6a088,1,2016-05-12 22:53:14
11,2,79c87ba31df14ec89f0da39e98b6a088,2,2016-05-12 22:53:14
12,2,79c87ba31df14ec89f0da39e98b6a088,3,2016-05-12 22:53:14
I'm having trouble coming up with the delete statement.
Best Answer
To solve this problem I did the following.
Created the table as per the question above. I then populated it with this data (a bit extra added for testing purposes).
First, we need to obtain the
c_id
s andtest_timestamp
s of the records we are going to keep:Result:
Now, we use these to find the
id
s corresponding to records with with thesec_id
s andtest_timestamp
s.Result:
These are the records we want to keep. You could just
SELECT t1.id
for only theid
- I just selected the extra fields for clarity.So, you might think that this query would work (it does in PostgreSQL)
But, due to the delightful (ahem...) quirks of MySQL's brand of SQL, instead of working, one is presented with this error message:
As it was put so aptly here "Sometimes I wonder what drugs the MySQL devs are on..." (found while searching for a solution - also to be found in that thread).
So, you have to add a further level of nesting to the query and finally, a query which works:
--
Result:
And run the
SELECT
to verify.Which should be the right answer - if this doesn't solve your issue, please clarify the question.