I want to keep the 10 latest duplicate rows and delete all the others.
I'm using the below code, but it deletes all the records except for one.
DELETE FROM `history` WHERE id NOT IN (SELECT * FROM (SELECT MIN(n.id) FROM history n GROUP BY n.url) x)`
Please try this query
This code will remove all duplicate row who have more then 10 duplicates.
DELETE FROM `history` WHERE id NOT IN (
SELECT * FROM (
SELECT MIN(n.id) FROM history n GROUP BY n.url having count(n.url) > 10
) x
)
Try this code that will work.
DELETE
FROM
history
WHERE
id NOT IN
(SELECT
id
FROM
(
SELECT
#num:=IF(#current=url, #num+1, 1) AS row_num,
#current:=url AS group_url,
id
FROM
history
ORDER BY
id
) AS `internal`
WHERE
row_num<=15
)
Related
I searched a lot and tried many queries but not getting satisfied answer. So like to ask.
I am looking for last 5 records from mysql table if having same value otherwise not.
Something like if col_n is having same value x from last 5 records then count otherwise not. But I am not able to figure out how to write query for this ?
SELECT count(col_n)
from track if(last five col_n = 'ok')
WHERE col_a = 'value1' AND col_b = 'value2'
enter mysql table records
Please try this:
SELECT p.*
FROM
( SELECT *
FROM demo ORDER by id DESC
LIMIT 5
) AS p
JOIN
( SELECT COUNT(*) AS cnt
FROM
( SELECT 1
FROM demo
LIMIT 5
) AS tmp
) AS c
ON c.cnt = 5 WHERE p.name='x'
I want to remove duplicate lines with the same badge_id and we check the user_id :
For example, with the following data:
id user_id badge_id badge_slot
1 2 ACH_1 0
2 1 ACH_1 0
3 1 ACH_1 0
4 1 AAAAA 0
How to delete ??
If you want to keep the records with the lowest id you can use min()
delete from your_table
where id not in
(
select * from
(
select min(id)
from your_table
group by user_id, badge_id
) x
)
And in MySQL you have the problem that you can't select from the same table that you are deleting from. But you can overcome this by a subselect.
Try this:
DELETE * ,count(*)as n your_table by badge_id HAVING n>1 .
hope it help!
Try this ..
ALTER IGNORE TABLE table ADD UNIQUE KEY idx1('badge_id','user_id');
I have a table with named "user-recent-activity" which has following columns: id, userid, activity and datetime. Now, I want to delete the records if any unique userid has more than 50 items, deleting the oldest records. For example, if the user id(lets say 1234) has more than 50 records in this table, then I have to save latest 50 records of user id(1234) and delete the oldest one.
Before inserting, query for the last 50 records with that ID (ordering from newer to older). If there is a 50th, substitute it (via update) instead of inserting a new row.
Assuming you are using a RDBMS that supports standard SQL the following stored procedure should do it.
create procedure remove-old-activities
(
#userid int
)
as
delete from user-recent-activity where userid=#userid and id not in (select top 50 id from user-recent-activity where userid=#userid order by datetime desc)
If you're DB does not support stored procedures then you should be able to use SQL parameters to pass the userid value...
Hope that helps
You could use rank method to precisely defined the rows number and thus delete the rows you want.
delete from tblName where id=
(select id from (
select #i := CASE WHEN ( #userid <> userid ) THEN 1
ELSE #i+1
END AS rank , id,userid, datetime2 ,#userid:=userid AS clset
from tblName x,(SELECT #i:=0) a ,(SELECT #userid:= 0) s
order by x.userid, datetime2 desc) T
where T.rank='50') ;
Another option:
Use the select query to select the rank <=50 and insert into a new table. Delete the old table and rename the new table afterwards.
insert into newtable (userid,activity,datetime2)
select userid,datetime2 from (
select #i := CASE WHEN ( #userid <> userid ) THEN 1
ELSE
#i+1
END AS rank , userid, activity,datetime2 ,#userid:=userid AS clset
from tblName x,(SELECT #i:=0) a ,(SELECT #userid:= 0) s
order by x.userid, datetime2 desc) T
where t.rank <=50
Im wondering if someone could help out.
I need to write a query that gets the last 3 'created' records, but their UID has to be unique so for example, my mysql fields look like so
uid created
19 2012-02-01 01:08:43
18 2012-02-31 17:07:21
19 2012-02-31 16:07:20
20 2012-02-31 13:03:00
Ok, so i want to get the last 3 uid's created ... but they have to be unique uid's so the same uid cant appear twice.
Cheers
this should do this
SELECT * FROM ( SELECT * FROM tablename ORDER BY uid, created DESC ) ordered GROUP BY uid
You can select last 3 IDs with this query:
SELECT * FROM ( SELECT * FROM table ORDER BY uid, created DESC ) AS selected GROUP BY uid LIMIT 3
Logic is: order table by uid field in descending and limit to 3 fields.
Use a Distinct and Group by to get what you're after:
SELECT DISTINCT * FROM table GROUP BY uid;
That will give you all unique UID's.
Then add your ORDER BY in there to make sure you grab the last records.
SELECT *
FROM table
ORDER BY created DESC
GROUP BY uid LIMIT 0,3
SELECT uid
, MAX(created) AS max_created
FROM tableX
GROUP BY uid
ORDER BY max_created DESC
LIMIT 3
I have a messcuts table with the following structure.
id, student_rollno, date.
The problem is there are some records duplicated ie. two records with same student_rollno in the same date. How do I remove them? Eg:
SELECT *
FROM `messcuts`
WHERE student_rollno = 'b070226'
|id |student_rollno|date
|259|B070226|2011-08-06
|260|B070226|2011-08-07
|1485|B070226|2011-08-12
|1486|B070226|2011-08-13
|1487|B070226|2011-08-14
|1488|B070226|2011-08-15
|2372|B070226|2011-08-27
|2369|B070226|2011-08-24
|2368|B070226|2011-08-23
|2371|B070226|2011-08-26
|2374|B070226|2011-08-29
|2373|B070226|2011-08-28
|2370|B070226|2011-08-25
|2367|B070226|2011-08-22
|2375|B070226|2011-08-30
|2376|B070226|2011-08-31
|2938|b070226|2011-08-06
See on 2011-08-06 there are two records.
select student_rollno, date
from messcuts
group by student_rollno, date
having count(*) > 1
and to delete:
delete from messcuts d where d.id in (
select max(s.id)
from messcuts as s
group by s.student_rollno, s.date
having count(*) > 1)
if not working in mysql:
delete from messcuts
using messcuts, messcuts as v_messcuts
where messcuts.id <> v_messcuts.id
and messcuts.student_rollno = v_messcuts.student_rollno
and messcuts.date = v_messcuts.date
This will show the duplicates
SELECT *
FROM messcuts
WHERE (student_rollno,date) IN
(
SELECT student_rollno,date
FROM messcuts
GROUP BY student_rollno,date
HAVING count(*)>1
)
and to delete:
CREATE VIEW dups AS
SELECT MAX(id) as id
FROM messcuts
GROUP BY student_rollno,date
HAVING count(*)>1
you will need to run this several times to get rid of all duplicates
DELETE FROM t2
WHERE id IN (SELECT id FROM dups)
once you have removed the duplicates It may be a good idea to add a unique constraint to stop furthur problems.
ALTER TABLE messcuts ADD UNIQUE std_dte (student_rollno,date)